如何在地图矢量中迭代地图?

Ala*_* M. 2 c++ maps iterator vector

用C++编写的代码

环境:Microsoft Visual Studio

我有一张地图矢量.首先,我想迭代第一个地图,得到它的"第一个"和"第二个"并将它们保存在我构建的其他结构中(矢量地图).然后我将在我的"地图矢量"中迭代左侧地图并执行相同的操作......

这是我的地图矢量:

typedef vector<map<string,unsigned int>> myvec;
Run Code Online (Sandbox Code Playgroud)

以下是应该完成工作的功能:

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)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

'for'循环应遍历向量中的第一个映射.我声明了一个地图迭代器,因为我需要它来迭代地图!对?为什么不工作?

错误:

IntelliSense:没有运算符"="匹配这些操作数

非常感谢 !!!


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

index::iterator idx_iter = the_index.begin();
Run Code Online (Sandbox Code Playgroud)

这是我的'索引':

typedef map<string,vector<unsigned int>> index;
Run Code Online (Sandbox Code Playgroud)

在提到的'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++;
    }
Run Code Online (Sandbox Code Playgroud)

#2似乎没问题.但#1会产生错误:

IntelliSense:没有运算符"="匹配这些操作数

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

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

再次:

typedef vector<map<string,unsigned int>> myvec;
typedef map<string,vector<unsigned int>> index;
Run Code Online (Sandbox Code Playgroud)

谢谢!

Xio*_*ion 7

the_vec作为常量的引用传递,所以你需要const_iterator:

map<string,unsigned int>::const_iterator map_iter;
Run Code Online (Sandbox Code Playgroud)