c++ - 我有一个map<string, vector<record>>,如何从vector<record>中删除记录?

标签 c++ dictionary vector segmentation-fault

我已经尝试了所有可能的方法,但未能找到解决方案。请帮忙

我想在迭代时删除 map 中 vector 中的记录。

map < string, vector < record> >::iterator map_it;
for(map_it = map_records.begin(); map_it != map_records.end(); ++map_it){
    vector < record>::iterator vec_it;
    for(vec_it = (*map_it).second.begin(); vec_it != (*map_it).second.end();){
        if(condition){
            cout << (*map_it).second.size() << endl;
            vec_it = map_it->second.erase(vec_it);
            cout << (*map_it).second.size()<< endl;
        } else {
            ++vec_it;
        }
    }
}

我尝试过这样的事情,
(*map_it).second.erase(vec_it)
如果我查询它的大小并且程序以段错误结束,它会给出一些很长的数字

输出:
18446744073709551615
18446744073709551615
段错误

感谢任何帮助

最佳答案

   vec_t = map_it->second.erase(vec_it); //note the assignment!

您还需要立即检查其有效性,因此最好将内部循环重写为:

for(vec_it = (*map_it).second.begin(); vec_it != (*map_it).second.end(); )
{
    if(condition)
        vec_it = map_it->second.erase(vec_it);
    else
      ++vec_it;
}

关于c++ - 我有一个map<string, vector<record>>,如何从vector<record>中删除记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314906/

相关文章:

arrays - 从向量的非零值获取相对中间索引

Android 矢量 Assets : Do dp measurements only matter for "wrap content"?

c++ - 返回元素是否比通过引用发送它们并在那里修改慢?

python - 获取与静态类型检查器一起使用的 TypedDict 值类型的函数

javascript - 从 2 个数组创建一个字典( MAP )

python - 需要解释 Python 中 json 和 dict 的区别

c++ - 如何根据要排序的 vector 中的 vector<string> 对结构 vector 进行排序?

c++ - 将 QString 转换为 char* 以用于第 3 方库的内存清理问题,如何解决?

c++ - va_arg 给出运行时错误

c++ - 如何在 native 应用程序中添加对 C++ 项目的 DLL 引用?