继续 this问题 我正在尝试访问 map 。但是我遇到了段错误。下面是我的代码:
typedef multimap<string, vector<string> > mos_map;
typedef multimap<string, vector<string> >::iterator mos_map_it;
int main()
{
mos_map mos;
mos_map_it it;
vector<string> v1;
v1.push_back("a");
v1.push_back("b");
v1.push_back("c");
v1.push_back("mo1");
mos.insert(mos_map::value_type(*(v1.end()-1),v1));
for(it=mos.begin();it!=mos.end();it++);
{
cout<<(*it).first<<endl;//seg fault occurs here
}
最佳答案
for(it=mos.begin();it!=mos.end();it++);
// ^
你的循环体是空的。
一些提示:
启用警告:
warning: for loop has empty body [-Wempty-body]
仅在需要时声明变量:
for(auto it = mos.begin(); it != mos.end(); it++); { cout << (*it).first << endl; }
这段代码会导致编译时错误:
error: use of undeclared identifier 'it'
关于c++ - 访问 map c++的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17362720/