简体   繁体   English

访问地图的分段错误C ++

[英]Segmentation fault which accessing a map c++

In continuation to this question I am trying to access a map. 为了继续这个问题,我正在尝试访问地图。 But i am getting a segmentation fault. 但是我遇到了细分错误。 Below is my code: 下面是我的代码:

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++);
//                                    ^

Your loop has empty body. 您的循环的主体为空。

Some tips: 一些技巧:

  • Enable warnings: 启用警告:

    warning: for loop has empty body [-Wempty-body] 警告:for循环的主体为空[-Wempty-body]

  • Declare variables only when they are needed: 仅在需要时声明变量:

     for(auto it = mos.begin(); it != mos.end(); it++); { cout << (*it).first << endl; } 

    This code will cause a compile time error: 此代码将导致编译时错误:

    error: use of undeclared identifier 'it' 错误:使用未声明的标识符“ it”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM