通过迭代器打印矢量的大小

时间:2011-01-24 12:39:38

标签: c++ vector map iterator

我正在尝试打印vector尺寸。听起来很简单,但矢量位于map。 目前我在地图上有一个迭代器,如下所示:
map<string, vector<map<vector<string> , vector<string> > > >::iterator it;

我试图显示这样的大小:


修改
迭代器的初始化如下:it = csvMap.find(commandList.at(lineCount));


cout<<"Size of vector in Map after modifying: " << it->second.size() <<"\n"<<endl;

它没有用,程序崩溃了。 我认为一种方法是制作一个临时矢量并用值it->second;填充它 但只是为了获得尺寸是一种浪费空间不是吗?

有更好的方法吗?

提前致谢!


EDIT2:删除旧代码


编辑3:新代码:

            map<vector<string> , vector<string> > parameterMap;
        parameterMap.insert(pair<vector<string> , vector<string> > (
            part1_input, part2_output));

        map<string, vector<map<vector<string> , vector<string> > > >::iterator it;

        cout<<"\nSize of CSV Map  before modifying: " << csvMap.size() <<endl;
        //cout<<"Size of vector in CSV Map  before modifying: " << it->second.size() <<"\n"<<endl;


        if(csvMap.size() == 0)
        {
            /*
            * csvMap is empty -> no need to search for something. Just insert the fist entries
            */
            listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
            csvMap.insert(pair<string, vector<map<vector<string> ,
                vector<string> > > > (commandList[lineCount],
                listedParameterMap));
            cout<<"CSV Map size: " << csvMap.size() <<endl;
        }
        else
        {
            /* 
            * Search if the Command is already available, if not,
            * add it to the map with its corresponding list of maps (in/output values)
            * find returns map::end if key is not found
            */

            cout<<"Checking if: " << commandList.at(lineCount) << " is already in the list \n" << endl;
            it = csvMap.find(commandList.at(lineCount));
            if (it == csvMap.end())
            {
                /*
                * it = csvMap.end() is true
                * The command isn't found
                */

                cout<< commandList.at(lineCount) << " command not available. Inserting it! \n" << endl;
                listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
                csvMap.insert(pair<string, vector<map<vector<string> ,
                vector<string> > > > (commandList[lineCount],
                listedParameterMap));
            }   
            else
            {
                /*
                * it != csvMap.end()
                * The command is found. Append the parameterMap to the vector in the map
                */
                cout<< commandList.at(lineCount) << " is already in the list! Appending parameters on pos: "<< it->second.size()-1<< "\n" << endl;
                it->second.push_back(parameterMap);
            }
        }
        cout<<"\nSize of CSV Map  after modifying: " << csvMap.size() <<endl;
        cout<<"Size of vector in CSV Map  after modifying: " << it->second.size() <<"\n"<<endl;

我希望有人还在读这个...

我现在发现it.second似乎是第一次交互时的问题。但我不明白为什么 代码段(也在上面的代码中):

if(csvMap.size() == 0)
        {
            /*
            * csvMap is empty -> no need to search for something. Just insert the fist entries
            */
            listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
            csvMap.insert(pair<string, vector<map<vector<string> ,
                vector<string> > > > (commandList[lineCount],
                listedParameterMap));
            cout<<"CSV Map size: " << csvMap.size() <<endl;
            cout<<"listedParameterMap: " << listedParameterMap.size() <<endl;
            cout<< commandList.at(lineCount) << " is already in the list! Appending parameters on pos: "<< it->second.size()<< "\n" << endl;
        }

这似乎不起作用。虽然它已经融入其中。知道为什么吗? 就我看来,comanndList和listedParameterMap就好了。

4 个答案:

答案 0 :(得分:3)

it = csvMap.find(commandList.at(lineCount));
if (it == csvMap.end()) {
  cout << "not found\n";
}
else {
  cout << "Size of vector in Map after modifying: " << it->second.size() << '\n';
}
  

当找不到命令或命令是最后一个

不,结束迭代器不是容器中的项目。

string c = (*it).first;

由于这是在迭代器是结束迭代器之后,因此在解除引用它时会有未定义的行为。

答案 1 :(得分:1)

您的it指向无效的位置。你需要使用map的迭代器来初始化它。像it = myMap.find("aaa"); //Replace it with actual key之类的东西在执行find之后,你需要通过再次检查myMap.end()来确保你拥有一个有效的迭代器。

修改

您在这里使用未初始化的迭代器:

cout<<"Size of vector in CSV Map  before modifying: " << it->second.size() <<"\n"<<endl;

另外,你不能取消引用指向csvMap.end()的迭代器,它会再次崩溃。

根据EDIT 3

您仍在使用指向if(csvMap.size() == 0)if(it == csvMap.end())情况结束的单位化迭代器/迭代器。您需要使用it函数的返回值初始化insert,如下所示: it = csvMap.insert(....).first;在这些情况下。

答案 2 :(得分:0)

除非您删除了该特定元素,否则映射迭代器不会失效。这意味着你做错了其他事。

答案 3 :(得分:0)

您的收藏相当复杂。

为了检查大小,你需要知道那里有一个元素,即find没有返回你地图的“end”。如果是,则不能使用返回的迭代器。

当然你如何处理这个问题是你自己的决定,例如,如果找不到字符串,则返回-1(0表示已找到但没有内容)。

现在您已经编辑了代码我立即发现了一个错误:

   if (it == csvMap.end())
            {

            /*
            * it = csvMap.end() > true
            * Either when command isn't found or command is the last 
            */

            string c = (*it).first;

你不能解除引用结束(it