我有由id代表的学生要在特定学期添加类(class)的功能。这段代码是正确的,但是当他这样做时,我不理解该部分for (size_t i = 0; i < (*iteratorVectorP).size(); i++) ...

void add_course(map<int, map<int, vector<course *> * > > &DB, int
     semester, int id, course c)
{
    auto iterator = DB.find(id);    //find id as key, set map to value
    vector<course*> *pointer = new vector<course*>;
    if (iterator != DB.end())
    {
        auto iterator1 = ((*iterator).second).find(semester);   //find
        semester as key, set vector to value
        if (iterator1 == (*iterator).second.end())      //if semester does not exist
        {
            pointer->push_back(new course(c));
            (iterator->second)[semester] = pointer;
        }
        else        //if semester exist
        {
            auto iteratorVectorP = (iterator->second)[semester];
            // i do not understand what his doing here
            for (size_t i = 0; i < (*iteratorVectorP).size(); i++)
            {
                if (c == *(*iteratorVectorP)[i])
                {
                    return;     //exit when class exist
                }
                else
                {
                    (*iteratorVectorP).push_back(new course(c));
                    return;
                }
            }
        }
    }
}

最佳答案

迭代器Vector的定义是 vector ,因此for循环正在迭代该过程,并尝试将其添加到 vector 中(如果不存在)。

10-06 11:16