如何使用基于范围的for()循环与std :: map?

这篇博客介绍了在C++11中如何使用基于范围的for循环处理std::map。文章指出,当遍历简单容器时,基于范围的for循环会直接提供每个元素,而不是迭代器。对于std::map,C++17引入了结构化绑定,可以直接访问键和值。在C++11和C++14中,可以使用增强的for循环逐个提取键值对。此外,还提到了使用boost库适配器来查看map的键或值。
摘要由CSDN通过智能技术生成

本文翻译自:How to use range-based for() loop with std::map?

The common example for C++11 range-based for() loops is always something simple like this: C ++ 11基于范围的()循环的常见示例总是这样简单:

std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7 };
for ( auto xyz : numbers )
{
     std::cout << xyz << std::endl;
}

In which case xyz is an int . 在这种情况下, xyz是一个int But, what happens when we have something like a map? 但是,当我们有像地图这样的东西时会发生什么? What is the type of the variable in this example: 此示例中变量的类型是什么:

std::map< foo, bar > testing = { /*...blah...*/ };
for ( auto abc : testing )
{
    std::cout << abc << std::endl;         // ? should this give a foo? a bar?
    std::cout << abc->first << std::endl;  // ? or is abc an iterator?
}

When the container being traversed is something simple, it looks like range-based for() loops will give us each item, not an iterator. 当遍历的容器很简单时,看起来基于范围的()循环将给我们每个项目,而不是迭代器。 Which is nice...if it was iterator, first thing we'd always have to do is to dereference it anyway. 哪个好...如果它是迭代器,我们总是要做的第一件事就是取消引用它。

But I'm confused as to what to expect when it comes to things like maps and multimaps. 但是,当涉及到地图和多重映射等内容时,我会感到困惑。

(I'm still on g++ 4.4, while range-based loops are in g++ 4.6+, so I haven't had the chance to try it yet.) (我仍然使用g ++ 4.4,而基于范围的循环是g ++ 4.6+,所以我还没有机会尝试它。)


#1楼

参考:https://stackoom.com/question/TDcs/如何使用基于范围的for-循环与std-map


#2楼

If copy assignment operator of foo and bar is cheap (eg. int, char, pointer etc), you can do the following: 如果foo和bar的复制赋值运算符很便宜(例如int,char,pointer等),则可以执行以下操作:

foo f; bar b;
BOOST_FOREACH(boost::tie(f,b),testing)
{
  cout << "Foo is " << f << " Bar is " << b;
}

#3楼

If you only want to see the keys/values from your map and like using boost, you can use the boost adaptors with the range based loops: 如果您只想查看地图中的键/值并使用boost,则可以将boost适配器与基于范围的循环一起使用:

for (const auto& value : myMap | boost::adaptors::map_values)
{
    std::cout << value << std::endl;
}

there is an equivalent boost::adaptors::key_values 有一个等价的boost :: adapters :: key_values

http://www.boost.org/doc/libs/1_51_0/libs/range/doc/html/range/reference/adaptors/reference/map_values.html http://www.boost.org/doc/libs/1_51_0/libs/range/doc/html/range/reference/adaptors/reference/map_values.html


#4楼

In C++17 this is called structured bindings , which allows for the following: 在C ++ 17中,这称为结构化绑定 ,它允许以下内容:

std::map< foo, bar > testing = { /*...blah...*/ };
for ( const auto& [ k, v ] : testing )
{
  std::cout << k << "=" << v << "\n";
}

#5楼

Each element of the container is a map<K, V>::value_type , which is a typedef for std::pair<const K, V> . 容器的每个元素都是map<K, V>::value_type ,它是std::pair<const K, V>typedef Consequently, in C++17 or higher, you can write 因此,在C ++ 17或更高版本中,您可以编写

for (auto& [key, value]: myMap) {
    std::cout << key << " has value " << value << std::endl;
}

or as 或者作为

for (const auto& [key, value]: myMap) {
    std::cout << key << " has value " << value << std::endl;
}

if you don't plan on modifying the values. 如果您不打算修改值。

In C++11 and C++14, you can use enhanced for loops to extract out each pair on its own, then manually extract the keys and values: 在C ++ 11和C ++ 14中,您可以使用增强型for循环来自行提取每对,然后手动提取键和值:

for (auto& kv : myMap) {
    std::cout << kv.first << " has value " << kv.second << std::endl;
}

You could also consider marking the kv variable const if you want a read-only view of the values. 如果您想要一个只读的值视图,您还可以考虑标记kv变量const


#6楼

From this paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2049.pdf 来自本文: http//www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2049.pdf

for( type-specifier-seq simple-declarator : expression ) statement

is syntactically equivalent to 在语法上等同于

{
    typedef decltype(expression) C;
    auto&& rng(expression);
    for (auto begin(std::For<C>::begin(rng)), end(std::For<C>::end(rng)); begin != end; ++ begin) {
        type-specifier-seq simple-declarator(*begin);
        statement
    }
}

So you can clearly see that what is abc in your case will be std::pair<key_type, value_type > . 所以你可以清楚地看到你的案例中的abcstd::pair<key_type, value_type > So for printing you can do access each element by abc.first and abc.second 因此,对于打印,您可以通过abc.firstabc.second访问每个元素

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值