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

本文翻译自: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 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::value_type , which is a typedef for std::pair . 容器的每个元素都是map::value_type ,它是std::pairtypedef 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::begin(rng)), end(std::For::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 . 所以你可以清楚地看到你的案例中的abcstd::pair So for printing you can do access each element by abc.first and abc.second 因此,对于打印,您可以通过abc.firstabc.second访问每个元素

你可能感兴趣的:(如何使用基于范围的for()循环与std :: map?)