【问题标题】:Print size of a vector through an iterator通过迭代器打印向量的大小
【发布时间】:2011-01-24 12:39:38
【问题描述】:

我正在尝试打印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;

它不工作,程序崩溃。 我认为一种方法是制作一个 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 似乎是第一次交互的问题。但我不明白为什么。
代码sn-p(也在上面的代码中):

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-&gt;second.size()之前检查if(it != csvMap.end())
  • 您确定您正在搜索的lineCount 确实存在吗?搜索后是否检查与map::end 的相等性?
  • @Space_C0wb0y lineCount 有效。我检查了。 @Naveen 是的,如果里面什么都没有,我认为它只是 0...
  • 我刚试过:即使我先拿出这个cout&lt;&lt;...it-&gt;second.size() ...,我只是在它确定存在时尝试打印,它不起作用

标签: c++ vector map iterator


【解决方案1】:
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;

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

【讨论】:

  • @Fred 看看我对 CashCows 的评论请回答
  • @Beasly:怎么样?正如我所说,结束迭代器不是容器中的项目。
  • @Fred 是的,我知道了。我只是告诉我为什么我认为它是这样的。那么 map::end 是一个空字段吗?
  • @Beasly:对于向量,end() 是 1 传递向量中的最后一个元素。对于其他容器,它是一个特殊的迭代器,指示容器中没有更多的值。
  • 对于向量,它指向 1 传递的最后一个元素。每当您对任何 STL 集合进行搜索(例如 find)并返回 end() 时,该元素都不在集合中。 end() 不是任何 STL 集合中的有效元素。
【解决方案2】:

您的 it 指向的位置无效。您需要使用地图的迭代器对其进行初始化。 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()) 情况下,您仍在使用指向结束的未初始化迭代器/迭代器。您需要使用insert 函数的返回值初始化it,如下所示: it = csvMap.insert(....).first; 在这些情况下。

【讨论】:

  • 根据您的 EDIT2,上述解释仍然有效。您正在使用未初始化的迭代器。此外,在使用迭代器时,发布的代码还有很多问题。
  • @Naveen find 方法工作正常,我检查了手表。够了吧?
  • @Naveen 请告诉我。我还在靠那些东西。我不太习惯迭代器
  • @Neveen 你是对的。弗兰克·努尔克也是。最后是两件事。似乎在某些时候我覆盖了我的交互器初始化并且我没有注意到。由于无法将 2 个答案标记为正确,因此我给了您 +1 :) 谢谢!
  • @Naveen 最后评论,它正在变成 OT ;)。这个社区真的很酷。几乎每次你得到一个非常快的响应。在大多数情况下有用的信息。像这样参加很有趣:) 下次见。我已经有了另一件有点连线的东西。也许明天;)
【解决方案3】:

除非您删除了该特定元素,否则 Map 迭代器不会失效。这意味着你做错了什么。

【讨论】:

    【解决方案4】:

    您的收藏相当复杂。

    为了检查大小,尽管您需要知道那里根本没有元素,即 find 没有返回地图的“结束”。如果是,则不能使用返回的迭代器。

    当然你如何处理这个是你自己的决定,例如如果没有找到字符串可能返回-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;
    

    你不能取消引用 end(it 是)

    【讨论】:

    • 我是从 API 中获取的:An iterator to the element, if the specified key value is found, or map::end if the specified key is not found in the container. 这就是为什么我认为没有找到它指向最后一个...
    • 当没有找到它时,它确实指向 map::end 但那不是最后一个元素,它是最后一个元素之后的一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多