44

我想遍历一个 STL 映射。我不想使用它的密钥。我不关心排序,我只是寻找一种访问它包含的所有元素的方法。我怎样才能做到这一点?

4

6 回答 6

72

是的,您可以遍历标准库map。这是用于遍历 a 的基本方法map,可作为遍历任何标准库集合的指南:

C++03/C++11:

#include <cstdlib>
#include <map>
#include <string>
using namespace std;

int main()
{
    typedef map<int,string> MyMap;
    MyMap my_map;
    // ... magic

    for( MyMap::const_iterator it = my_map.begin(); it != my_map.end(); ++it )
    {
      int key = it->first;
      string value = it->second;
    }
}

如果需要修改元素:

  • 使用iterator而不是const_iterator.
  • 与其从迭代器中复制值,不如获取引用并通过它修改值。

    for( MyMap::iterator it = my_map.begin(); it != my_map.end(); ++it ) { int key = it->first; 字符串&值=它->秒;如果(价值==“富”)价值=“酒吧”;}

这就是您通常手动遍历标准库容器的方式。最大的区别是对于 amap的类型*it是 apair而不是元素本身

C++11

如果您受益于 C++11 编译器(例如,带有--std=c++11或 MSVC 的最新 GCC),那么您还有其他选择。

首先,您可以使用auto关键字来摆脱所有令人讨厌的冗长:

#include <cstdlib>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for( auto it = my_map.begin(); it != my_map.end(); ++it )
    {
      int key = it->first;
      string& value = it->second;
    }
}

其次,您还可以使用 lambda。与 结合使用decltype,这可能会产生更简洁的代码(尽管需要权衡):

#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for_each(my_map.begin(), my_map.end(), [](decltype(*my_map.begin()) val)
    {
        string& value = val.second;
        int key = val.first;
    });
}

C++11 还引入了基于范围的for循环的概念,您可能会认为它与其他语言相似。然而,一些编译器还不完全支持这一点——尤其是 MSVC。

#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for(auto val : my_map )
    {
        string& value = val.second;
        int key = val.first;
    }
}
于 2010-11-17T17:37:15.360 回答
11

与任何 STL 容器一样,begin()andend()方法返回可用于迭代映射的迭代器。取消引用映射迭代器会产生一个std::pair<const Key, Value>.

于 2010-11-17T17:37:26.287 回答
7

C++17

C++17 开始,您可以使用基于范围的 for 循环结构化绑定来迭代地图。生成的代码(例如用于打印地图的所有元素)很短且易读:

std::map<int, std::string> m{ {3, "a"}, {5, "b"}, {9, "c"} };

for (const auto &[k, v] : m)
    std::cout << "m[" << k << "] = " << v << std::endl;

输出:

m[3] = a
m[5] = b
m[9] = c

Coliru 上的代码

于 2019-03-27T07:46:14.853 回答
5

您可以像任何其他 STL 容器一样遍历STL 映射:使用迭代器,例如

for (std::map<key, value>::const_iterator
     i = myMap.begin(), end = myMap.end(); i != end; ++i)
{
    // *i is a key-value pair
}
于 2010-11-17T17:39:21.820 回答
1

使用forwithauto用于 C++11 及更高版本的用法

map<int,int> map_variable; //you can use any data type for keys, as well as value

for(auto &x:map_variable)
{ 
    cout<<x.first ;// gives the key
    cout<<x.second; //gives the value
}

在 C++11 中引入了新的forusing格式auto

赋予它像python这样的高级语言的功能

已经实现了这种类型的迭代

PS:map变量保持值排序,所以在迭代时你会得到排序顺序的键

于 2018-10-16T14:13:18.257 回答
0

您可以使用自动迭代器来迭代地图。

代码片段:

#include<bits/stdc++.h>
using namespace std;

int main()
{
      ios::sync_with_stdio(false);
      map<string, int> mp;

      mp["a"]=500;
      mp["b"]=200;
      mp["d"]=300;
      mp["c"]=400;

      for(auto it=mp.begin(); it != mp.end(); it++)
      {
         cout<<it->first <<" : "<<it->second<<endl;
      }
      return 0;
}
于 2017-07-08T12:07:30.273 回答