语法:

 for ( for-range-declaration : expression )
statement

注意一般用auto表达类型。不需要修改时常用引用类型

例子:

 // range-based-for.cpp
// compile by using: cl /EHsc /nologo /W4
#include <iostream>
#include <vector>
using namespace std; int main()
{
// Basic 10-element integer array.
int x[] = { , , , , , , , , , }; // Range-based for loop to iterate through the array.
for( int y : x ) { // Access by value using a copy declared as a specific type.
// Not preferred.
cout << y << " ";
}
cout << endl; // The auto keyword causes type inference to be used. Preferred. for( auto y : x ) { // Copy of 'x', almost always undesirable
cout << y << " ";
}
cout << endl; for( auto &y : x ) { // Type inference by reference.
// Observes and/or modifies in-place. Preferred when modify is needed.
cout << y << " ";
}
cout << endl; for( const auto &y : x ) { // Type inference by reference.
// Observes in-place. Preferred when no modify is needed.
cout << y << " ";
}
cout << endl;
cout << "end of integer array test" << endl;
cout << endl; // Create a vector object that contains 10 elements.
vector<double> v;
for (int i = ; i < ; ++i) {
v.push_back(i + 0.14159);
} // Range-based for loop to iterate through the vector, observing in-place.
for( const auto &j : v ) {
cout << j << " ";
}
cout << endl;
cout << "end of vector test" << endl;
}

基于范围的for循环的更多相关文章

  1. C++11基于范围的for循环

    C++11包含一种新的 for 循环,称为基于范围的 for 循环,可以简化对数组元素的遍历.格式如下: for(Type VarName : Array){ //每个元素的值会依次赋给 VarNam ...

  2. Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 循环队列

    Atitit.升级软件的稳定性---基于数据库实现持久化  循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...

  3. C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)

    3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...

  4. 基于范围的for循环(STL)

    1. ]={4.99,5.99,6.99,7.99,8.99}; for (double x : prices) cout<<x<<endl; //////////////// ...

  5. 基于范围的for循环(C++11)

    C++11新增了一种循环:基于范围的for循环.这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,如下例所示 for语句允许简单的范围迭代:(只遍历, ...

  6. 第9课 基于范围的for循环

    1. 基于范围的for循环(range-based for) (1)语法:for(decl : coll){//statement} ①decl用于声明元素及类型,如int elem或auto ele ...

  7. c++11 基于范围的for循环

    c++11 基于范围的for循环 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> # ...

  8. c++11——基于范围的for循环

    c++11中有基于范围的for循环,基于范围的for循环可以不再关心迭代器的概念,只需要关系容器中的元素类型即可,同时也不必显式的给出容器的开头和结尾. int arr[] = {1, 2, 3, 4 ...

  9. c++11之一: 基于范围的for循环

    #include <iostream> using namespace std; int main(){ ]{,,,,}; for (int& e: ary) e *= ; for ...

  10. C++11特性中基于范围的for循环

    本文摘录柳神笔记:   除了像C语⾔的for语句 for (i = 0; i < arr.size(); i++) 这样,C++11标准还为C++添加了⼀种新的 for 循环⽅ 式,叫做基于范围 ...

随机推荐

  1. python中的关键字符

    from keyword import kwlistprint(kwlist)for i in kwlist: print(i) 可以显示所有的关键字符,开发者不要重新赋予其他值. a = 10000 ...

  2. 如何创建一个 Lua 模块

    如何创建一个 Lua 模块 翻译自: How to Create a Lua Module 译者: FreeBlues 正文 Lua编程语言 中的一个 模块(module)是一个包含函数和变量的代码片 ...

  3. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

  4. Java并发编程原理与实战三十九:JDK8新增锁StampedLock详解

    1.StampedLock是做什么的? ----->它是ReentrantReadWriteLock 的增强版,是为了解决ReentrantReadWriteLock的一些不足.   2.Ree ...

  5. R6—单变量正态性检验

    方法不唯一 单变量正态检验主要的话包括以下这些 shapiro.test();#Shapiro-Wilk检验 library("nortest"); lillie.test() # ...

  6. 【leetcode 简单】 第一百一十一题 可怜的小猪

    有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 1005 HDU 6154 CaoHaha's staff (找规律)

    题目链接 Problem Description "You shall not pass!" After shouted out that,the Force Staff appe ...

  8. sql server 查询本周、本月所有天数的数据

    查询本月所有的天数: --本月所有的天数 ),) day from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询本周所有的天数: ),,),) ),,),) ...

  9. 在Linode VPS上搭建离线下载神器Aria2+WEBUI管理及对国内云盘看法

    在Linode VPS上搭建离线下载神器Aria2+WEBUI管理及对国内云盘看法 2015-09-21 by Hansen 原文链接:http://www.hansendong.me/archive ...

  10. PHP内存溢出 Allowed memory size of 解决办法

    PHP出现如下错误:Allowed memory size of  xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes)    关于 ...