如何使用BOOST_FOREACH与Unordered_map? [英] How to use BOOST_FOREACH with an Unordered_map?

查看:261
本文介绍了如何使用BOOST_FOREACH与Unordered_map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这里是我的情况 - pretty简单,但我不知道它如何能工作(我找不到任何文档......):

OK, so here's my situation - pretty straightforward but I'm not sure how it can work (I can find no documentation whatsoever...) :

我有一个 Unordered_map

typedef unsigned long long U64;
typedef boost::unordered_map<U64, U64> HASH;

和我想通过要素(主要是键),就像使用PHP 的foreach ,但使用 BOOST_FOREACH ,我怀疑是这样的:

And I would like to loop through the elements (mainly the keys), pretty much like using PHP foreach, but this time using BOOST_FOREACH, I suspect something like :

HASH myMap;

// .. assignment, etc...

BOOST_FOREACH (U64 key, myMap)
{
     // do sth with the Key-Value pair

     U64 val = myMap[key];
}


任何想法?


Any ideas?

推荐答案

在每个条目的 Unordered_map 将一对,所以当您使用一起选择的地图 BOOST_FOREACH 你会遍历那双像这样:

Each entry in the Unordered_map will be a pair, so when you use the map in conjuction with BOOST_FOREACH you will iterate over that pair like so:

BOOST_FOREACH( HASH::value_type& v, myMap ) {
    std::cout << "key is " << v.first << " value is " << v.second << std::endl;      
}

这篇关于如何使用BOOST_FOREACH与Unordered_map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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