for-each 循环

C ++中基于范围的循环(增强了循环) (Range-based loop in C++ (enhanced for loop))

for loop is used to execute a block of statements multiple times if the user knows exactly how many iterations are needed or required.

如果用户确切知道需要或需要多少次迭代,则使用for循环多次执行语句块。

After the release of C++ 11, it supports an enhanced version of for loop, which is also called for-each loop or enhanced for loop. This loop works on iterable like string, array, sets, etc.

C ++ 11发行后,它支持for循环增强版本,也称为for-each循环增强的for循环 。 此循环适用于可迭代的字符串,数组,集合等。

Syntax of range-based (for-each/enhanced for loop):

基于范围的语法(for-each / enhanced for loop):

    for (data_type variable : iterable){
//body of the loop;
}

It stores each item of the collection in variable and then executes it.

它将集合的每个项目存储在变量中,然后执行它。

Note: auto keyword can be used instead of data_type which automatically deduce the type of the element. So type error can be reduced.

注意:可以使用auto关键字代替data_type ,它会自动推断元素的类型。 因此可以减少类型错误。

C ++代码演示基于范围的循环的示例 (C++ code to demonstrate example of range-based loop)

// C++ program to demonstrate example of
// range-based loop (for-each/ enhanced for loop)
#include <iostream>
using namespace std;
int main()
{int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << "\n Demonstration of for-each in C++" << endl;
// Notice that instead of int, auto is used
// it automatically checks for the type of
// the variable so type error can be reduced
// using auto keyword
for (auto x : arr){cout << " " << x << endl;
}
return 0;
}

Output

输出量

Demonstration of for-each in C++
1
2
3
4
5
6
7
8
9

翻译自: https://www.includehelp.com/stl/range-based-loop-in-cpp-similar-to-for-each-loop.aspx

for-each 循环

for-each 循环_C ++中基于范围的循环(类似于for-each循环)相关推荐

  1. python什么语句提前结束循环_在循环语句中,break语句的作用是提前结束循环。

    [单选题]__________ sind Sie? Ich bin Studentin. [填空题]_______ Internet [单选题]()函数可以返回x的整数部分? [单选题]_______ ...

  2. Java中return结束循环,Java中break、continue、return在for循环中的使用

    Java中break.continue.return在for循环中的使用 发布时间:2020-09-25 02:01:45 来源:脚本之家 阅读:81 作者:mrr 引言:在使用循环的时候,循环里面带 ...

  3. java for循环break_Java中break、continue、return在for循环中的使用

    这篇文章主要介绍了break.continue.return在for循环中的使用,本文是小编收藏整理的,非常具有参考借鉴价值,需要的朋友可以参考下 引言:在使用循环的时候,循环里面带有break.co ...

  4. c foreach循环_C ++中的foreach循环

    c foreach循环 介绍 (Introduction) The foreach loop in C++ or more specifically, range-based for loop was ...

  5. Python循环语句中的else

    Python循环语句中的else 简介 流程图 作用 简介 在while和for循环语句中都可以跟上else语句,但是与if-else判断不同.在这里,else是循环语句正常结束时才能执行的代码段,如 ...

  6. for循环在c++中的用法_C ++中的循环

    for循环在c++中的用法 Loops come into picture when we need to execute a particular action in a repeated mann ...

  7. c语言 循环嵌套循环_C中的嵌套循环

    c语言 循环嵌套循环 Here you will learn about nested loops in C. 在这里,您将了解C中的嵌套循环. Nesting is one of the most ...

  8. 基于数组实现循环队列(基于Java实现)

    title: 基于数组实现循环队列(基于Java实现) tags: 数组 循环队列 基于数组实现循环队列的方法原理: 我们在用数组实现队列的时候,发现当tail = n时,就会有数据搬移的操作,这样一 ...

  9. linux进程看门狗使用方式,Linux系统中基于看门狗的精细化进程监控方法及系统的制作方法...

    Linux系统中基于看门狗的精细化进程监控方法及系统的制作方法 [技术领域] [0001] 本发明涉及Linux系统的进程监控技术领域,特别是涉及一种Linux系统中基于看 门狗的精细化进程监控方法及 ...

最新文章

  1. 【直播】陈安东,但扬:CNN模型搭建、训练以及LSTM模型思路详解
  2. vim与gvim技巧大全
  3. IDEA 出现 updating indices 卡进度条问题的解决方案并加快索引速度
  4. 通过输入流获取的xml格式字符串转为json和map格式
  5. 数组实例的find()和findIndex()
  6. BZOJ4825: [Hnoi2017]单旋(Splay)
  7. 求qt如何解决小数计算过程中的精度丢失
  8. mysql - binlog主从复制
  9. python根据日期生成动态密码
  10. python外部库是什么_Python 常用外部模块详解
  11. 【转】基金入门:如何买基金?
  12. DC入门(一)综合基础
  13. 美国次贷危机的演变过程
  14. 快速画正弦波、方波、三角波——Visio制图总结(六)
  15. 在虚拟机中安装kali
  16. React从入门到精通教程01
  17. 导航栏地址中HTTPS是红色什么原因?
  18. 判断一个链表中是否有环
  19. 帐套和会计科目的理解
  20. ubuntu使用再生龙进行系统盘克隆

热门文章

  1. linux如何进windows系统还原,怎样进行系统修复,还原?具体步骤是什么?
  2. 轻舟已过万重山:专访网易云陈谔
  3. 立即执行函数报错Uncaught TypeError: (intermediate value) is not a function.
  4. 基于stm32的俄罗斯方块游戏机(6、游戏机整体的操作页面)(大三寒假)
  5. 百度输入法皮肤工具提示 CSS,【图片】【教程】百度手机输入法皮肤模板的制作_百度手机输入法吧_百度贴吧...
  6. 线下活动抽奖PHP源码,对接公众号,实现关注公众号发送关键字获取抽奖码,通过平台进行摇号抽奖,可以二次开发(所有公众号都可对接)
  7. 机器学习之多元线性回归
  8. GPS / GPRS 车载监控终端的设计与实现
  9. ubuntu中U盘硬盘格式化NTFS
  10. 《三个×××手》BD-R/960M[中字]1024分辨率 高清下载