c++ - 访问 map c++的段错误

标签 c++ dictionary

继续 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/

相关文章:

c++ - 如何在 C++ 中获取 .csv 文件的一部分?

c++ - 这种文字颜色有什么问题?

python - 我可以使用生成器表达式来打印字典列表的键、值对吗?

algorithm - 删除多个和大型词典文件中的重复单词

Python:创建带有书面报告路线的 png map 的最佳方法是什么?

c++ - 在 C++ 中的 3D map 中插入值

c++ - 使用 fopen 读取 C++ 中的文本文件,无需换行转换

c++ - 最小平均重量周期——直观解释

python - 通过键列表访问嵌套字典项?

c++ - 无法删除 unsigned char* 数组