如何在都包含指针的地图内创建列表?
How to create a list inside a map where both contain pointers?
我正在做一个需要
的项目
class course;
void add_student(map<int, map<int, list<course *> * > *> &DB, int id);
所以当我检查课程没有呈现时,我想创建一个列表。这是我的代码,
if(semesterIt == studentIt->second->end()){
DB[id][semester] = new list<course *>();
}
但是当我运行它时,编译器给我这个错误
no viable overloaded '='
不知道如何解决。 :(
DB[id][semester] = new list<course *>();
在语法上是错误的,因为 DB[id]
求值为指针,而不是对象或引用。
我的建议:
auto& mapPtr = DB[id];
if ( mapPtr == nullptr )
{
mapPtr = new map<int, list<course *> * >;
// Not necessary since mapPtr is a reference to the element.
// DB[id] = mapPtr;
}
auto& course_list_ptr = (*mapPtr)[semester];
if ( course_list_ptr == nullptr )
{
course_list_ptr = new list<course*>;
// Again, not necessary.
// (*mapPtr)[semester] = course_list_ptr;
}
我正在做一个需要
的项目class course;
void add_student(map<int, map<int, list<course *> * > *> &DB, int id);
所以当我检查课程没有呈现时,我想创建一个列表。这是我的代码,
if(semesterIt == studentIt->second->end()){
DB[id][semester] = new list<course *>();
}
但是当我运行它时,编译器给我这个错误
no viable overloaded '='
不知道如何解决。 :(
DB[id][semester] = new list<course *>();
在语法上是错误的,因为 DB[id]
求值为指针,而不是对象或引用。
我的建议:
auto& mapPtr = DB[id];
if ( mapPtr == nullptr )
{
mapPtr = new map<int, list<course *> * >;
// Not necessary since mapPtr is a reference to the element.
// DB[id] = mapPtr;
}
auto& course_list_ptr = (*mapPtr)[semester];
if ( course_list_ptr == nullptr )
{
course_list_ptr = new list<course*>;
// Again, not necessary.
// (*mapPtr)[semester] = course_list_ptr;
}