c++ - 如何遍历 map vector 中的 map ?

标签 c++ vector iterator maps

用C++编写的代码

环境:Microsoft Visual Studio

我有一个 map vector 。 首先,我想遍历第一张 map ,获取它的“第一张”和“第二张”并将它们保存在我构建的其他一些结构中( vector map )。 然后我将遍历我的“ map vector ”中的左侧 map 并执行相同的操作...

这是我的 map vector :

typedef vector<map<string,unsigned int>> myvec;

下面是应该完成这项工作的函数:

void Coogle::make_index(const myvec& the_vec)
{
    //SCAN THE FIRST MAP
    map<string,unsigned int>::iterator map_iter;
    index::iterator idx_iter = the_index.begin();
    for(map_iter=the_vec[0].begin(); map_iter!=the_vec[0].end(); ++map_iter)
    {

    }
}

“for”循环应该遍历 vector 中的第一个 map 。 我声明了一个 map 迭代器,因为我需要它来遍历 map !正确的? 为什么它不起作用?

错误:

IntelliSense: no operator "=" matches these operands

非常感谢!!!


现在我确定了这个迭代器:

index::iterator idx_iter = the_index.begin();

这是我的“索引”:

typedef map<string,vector<unsigned int>> index;

在提到的“for”循环中,我执行了以下操作:

    for(map_iter=the_vec[0].begin(); map_iter!=the_vec[0].end(); ++map_iter)
    {
        /*#1*/ idx_iter->first = map_iter->first;
        /*#2*/ idx_iter->second[0] = map_iter->second;
        /*#3*/ idx_iter++;
    }

#2 似乎没问题。但是 #1 产生了一个错误:

IntelliSense: no operator "=" matches these operands

这和之前的错误一样,所以我猜这是一个类似的问题。 是吗?

编辑:为了更清楚,我想做的是将 const myvec& the_vec 添加到“i”位置(在本例中为“0”)到我的索引。

再次:

typedef vector<map<string,unsigned int>> myvec;
typedef map<string,vector<unsigned int>> index;

谢谢!

最佳答案

the_vec 作为对常量的引用传递,因此您需要 const_iterator:

map<string,unsigned int>::const_iterator map_iter;

关于c++ - 如何遍历 map vector 中的 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12005727/

相关文章:

performance - 附加到向量的效率

c++ - Qt 图表 - 如何在 x 和 y 轴上显示特定点值?

c++ - 沿枚举 C++ 返回变量类型

c++ - 如何强制 QLocale::system 改变

java - 在 Java 中单击时尝试删除屏幕上的对象不起作用

c++ - Gtkmm:将 RefPtr 与保存在 std::vector 中的小部件一起使用

arrays - 给定一个都具有字段 foo 的结构数组,是否有一种简单的方法来获取 foo 值的数组?

java - (java) 无法应用迭代器中的删除

java - 如何将迭代器转换为流?

c++ - 调用线性化后奇怪的 boost 循环缓冲区行为