【发布时间】:2017-07-24 22:36:00
【问题描述】:
我是新来的。我是 C++ 中的迭代器(或者更确切地说是 STL)的新手。我正在尝试以循环方式遍历地图的键。因此,我们从头开始阅读,继续阅读到最后,然后再次回到起点。下面的代码是我程序相关部分的简化:
#include<iostream>
#include<map>
using namespace std;
int main(int argc, char* argv[])
{
map<const char*, int> colors;
colors = { { "RED", 1 },
{ "YELLOW", 2 },
{ "GREEN", 3 },
{ "ORANGE", 4 },
{ "CYAN", 5 } };
map<const char*, int>::iterator itr = colors.begin();
for(int i=0; i<10; i++) // Loop more than entries in map
{
cout<<itr->first<<endl;
if(itr==colors.end())
itr = colors.begin(); //start from beginning
else
itr++;
}
return 0;
}
我的程序(和上面的程序)在遍历地图一次后一直崩溃。我不知道为什么。我尝试在 SO 和其他地方查找,但找不到解决方案。
提前致谢。
【问题讨论】:
标签: c++