从常量迭代器到映射向量,获取映射向量中映射元素的键和值
Get the key and values of map elements in a vector of maps from a constant iterator to the vector of maps
我有一个包含字符串的映射向量。,即
vector< map <string,string> > vectorOfMaps;
vector< map <string,string> >::const_iterator itr =vectorOfMaps.begin();
vectorOfMaps填充在另一个函数中,调用方函数只能访问const_iterator itr。
如何访问vectorOfMaps中每个map元素的键及其各自的值?
感谢任何帮助:)
编辑:找到我的解决方案了。
map<string,string> myMap = (*itrVectorOfMaps);
while(loop till the end element)
{
for(map<string,string>::iterator itM = myMap.begin();
itM != myMap.end(); itM++)
{
cout<<"Key="<<itM->first<<" => Value="<<itM->second<<endl;
}
itrVectorOfMaps++;
myMap=(*itrVectorOfMaps);
}
在map
的vector
上迭代时,可以使用first
和second
关键字来访问map
元素。
for(auto const& currentMap : vectorOfMaps) // Loop over all the maps
{
for(auto const& element : currentMap) // Loop over elements of current map
{
std::string const& key = element.first;
std::string const& value = element.second;
}
}
您的解决方案很糟糕,因为您制作了多个映射副本,第一个在循环之前,然后在循环内部。考虑一下这个更短更快的版本:
for (auto const& el: *itrVectorOfMaps)
cout << "Key=" << el.first << " => Value=" << el.second << endl;
相关文章:
- 如何在 c++ 中创建结构向量的映射
- 在C++中初始化向量映射的最有效方法
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 字符串共享内存映射的向量
- 具有 2 个分量的数组的特征映射到 3 的向量
- 当键值是 std 向量时,为什么使用 at in C++ 访问映射值如此缓慢?
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 如何初始化 std::向量的映射?
- 访问映射中的分区向量
- 如何将整数向量插入到键中,标准::映射的值
- 将一个向量映射到不同的对,即使向量映射到每对时是不同的?
- 如何从向量映射中迭代向量
- 访问映射向量的映射元素(c++)
- 映射向量的时间复杂性是多少
- C++:通过从映射向量分配映射实例来填充映射的映射
- 在一个映射向量上迭代,如果满足条件,则插入一个副本
- 如何在映射向量中循环访问地图
- 丢弃限定符 访问类中的映射向量时出错
- 从常量迭代器到映射向量,获取映射向量中映射元素的键和值
- 映射向量c++