用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/