如何遍历/迭代STL地图? [英] How to traverse/iterate STL Map?

查看:113
本文介绍了如何遍历/迭代STL地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要知道如何遍历stl地图。我不想用它的钥匙。我不在乎排序,只是一种方法来访问它包含的所有元素。有办法吗?

解决方案

当然。这是用于遍历标准库集合的基本方法:



C ++ 03 / C ++ 11:



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

int main()
{
typedef map 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;
string& value = it-> second;
if(value ==foo)
value =bar;
}

这是你通常用手遍历容器的方法。最大的区别是对于映射类型 * it code>而不是元素本身



C ++ 11



一个C ++ 11编译器(例如,最新的GCC, - std = c ++ 11 或MSVC),那么你也有其他选择。

首先,您可以使用 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;
}
}

其次,您也可以使用lambdas。结合 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;
}
}


In need to know how to traverse an stl map. I don't want to use its key. I don't care about the ordering, just a way to access all elements it contains. Is there a way to do this?

解决方案

Sure. This is the basic method used to traverse Standard Library collection:

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;
    }
}

If you need to modify the elements, use iterator rather than const_iterator, and instead of copying the values out of the iterator, get a reference and modify the values through that:

for( MyMap::iterator it = my_map.begin(); it != my_map.end(); ++it )
{
  int key = it->first;
  string& value = it->second;
  if( value == "foo" )
    value = "bar";
}

This is how you typically traverse containers by hand. The big difference is that for a map the type of *it is a pair rather than the element itself

C++11

If you have the benefit of a C++11 compiler (for example, latest GCC with --std=c++11 or MSVC), then you have other options as well.

First you can make use of the auto keyword to get rid of all that nasty verbosity:

#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;
    }
}

Second, you can also employ lambdas. In conjunction with decltype, this might result in cleaner code (though with tradeoffs):

#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 also instroduces the concept of a range-bases for loop, which you may recognize as similar to other languages. However, some compilers do not fully support this yet -- notably, 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;
    }
}

这篇关于如何遍历/迭代STL地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆