本文翻译自: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访问每个元素

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

  1. ssis for循环容器_SSIS Foreach循环与For循环容器

    ssis for循环容器 In this article, first, we will briefly describe foreach loops and for loops. Then, we ...

  2. php in循环与for循环,详谈js中标准for循环与foreach(for in)的区别

    js中遍历数组的有两种方式 var array=['a'] //标准的for循环 for(var i=1;i alert(array[i]) } //foreach循环 for(var i in ar ...

  3. for循环与while循环效率对比·5年以下编程经验必看C#】

    for效率测试代码: using System; using System.Diagnostics; namespace Action {class Program{static void Main( ...

  4. python range函数怎么表示无限_Python for循环与range函数的使用详解

    for 循环 for - in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(iterates),即它会遍历序列中的每一个项目 注意: 1.else 部分是可选的.当循环中包含它时,它循环中 ...

  5. range函数python-Python for循环与range函数的使用详解

    for 循环 For - in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates),即它会遍历序列中的每一个项目 注意: 1.else 部分是可选的.当循环中包含它时,它循环中 ...

  6. Python 中的循环与 else

    1. 含义 Python 中的循环与 else 有以下两种形式 for - else while - else Python中的 for.while 循环都有一个可选(optional)的 else ...

  7. LabView学习笔记(六):while循环与for循环

    Labview学习笔记: LabView学习笔记(一):基础介绍 LabView学习笔记(二):滤波器实验 LabView学习笔记(三):基本控件 LabView学习笔记(四):动态数据类型 LabV ...

  8. C语言之for循环与while循环

    文章目录 一.单层for循环 二.for循环与if选择的嵌套 三.多层for循环的嵌套 四.while循环 五.总结 一.单层for循环 引例:C语言实现求1到10的和(用for循环实现) #incl ...

  9. 基于STM32F103的红外循迹避障小车设计(含Proteus仿真)

    基于STM32F103的红外循迹避障小车设计 红外循迹及红外避障实现较简单,无论是51单片机还是STM32单片机,其例程随处可见.但是完全可以运行的Proteus仿真,开源的并不多,更不要说基于STM ...

最新文章

  1. 【C#】Gif文件生成
  2. 详解操作主机角色,Active Directory系列之九
  3. 线程访问临界区的问题 实例,需解决
  4. Visual Studio2005无法启动web调试的真正原因
  5. 《Excel 职场手册:260招菜鸟变达人》一第 20 招 怎样在单元格文字前加空白
  6. 四十五、Gtihub+Hexo+icarus搭建自己的博客
  7. “美登杯”上海市高校大学生程序设计邀请赛 **D. 小花梨的取石子游戏**
  8. 单片机中存储器扩展位地址线怎么算_51单片机存储器小结
  9. python编程资料包_强力推荐,非常全的Python编程400集学习资料(今日限免)
  10. 电脑自建服务器tomcat,怎么配置搭建tomcat服务器
  11. 快递电子面单接口:四通一达,百世,邮政,顺丰,德邦等快递电子面单接口免费接入
  12. 神仙软件商店:到这儿买软件会员最多打5折,各种绿色开源软件还应有尽有
  13. catia v5法矢数据软件_CATIA V5 Start Model车身建模
  14. 湖南大学应用经济学考研考情与难度、参考书及上岸前辈备考经验
  15. 从零开始构建嵌入式实时操作系统2——重构
  16. 什么是流批一体化、区块链
  17. 台式计算机网卡型号怎么查找,台式机无线网卡如何查看型号
  18. SSM与Spring
  19. ListView 联动 SideBar ,根据数据首字母进行排序
  20. F:跳蛙【2019北大夏令营F】

热门文章

  1. 查看80端口连接状态
  2. 标准布局类(11中布局类)
  3. 数组练习:各种数组方法的使用
  4. 使用Apache + knewcode,用传统C++构建Web网站
  5. 另类的缓存技术(存储数据)
  6. JavaWeb项目开发案例精粹-第6章报价管理系统-001需求分析及设计
  7. Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能[转]
  8. Linux 内核 3.8 是给 Linux 用户的圣诞礼物
  9. GNOME 2.18.2
  10. 企业运维经典面试题汇总(3)