c++ - 通过迭代器打印 vector 的大小

标签 c++ vector dictionary iterator

我正在尝试打印 vector大小 .听起来很简单,但 vector 在 map 中. 目前我在 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;

它不工作,程序崩溃了。 我认为一种方法是制作一个temp vector 并用值 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;
        }

这似乎行不通。虽然它正在进入它。知道为什么吗? commanndList 和 listedParameterMap 就我看来是可以的。

最佳答案

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';
}

Either when command isn't found or command is the last

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

string c = (*it).first;

因为这是在迭代器之后的结束迭代器,所以在取消引用它时会有未定义的行为。

关于c++ - 通过迭代器打印 vector 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4782051/

相关文章:

c++ - STL 自定义分配器

c++ - 使用 memcpy、endian 将 unsigned long 转换为 std::vector?

list - 如何检查列表是否包含Dart中的特定值?

c++ - Windows下如何编译Botan和Qt?

c++ - 如何向基类注册派生类成员函数指针

c++ - 用C++快速读写文件

c++ - 从 C 数组(主要列)转换为 Armadillo 矩阵(arma::mat)而不复制

向量列表中的 R 求和元素 X

c++11 - 带有映射的结构的 ostream 运算符重载

c# - 如何从对象中的字典访问值?