相关资料:https://legacy.gitbook.com/book/changkun/cpp1x-tutorial/details

C++11之for循环的新用法

C++使用如下方法遍历一个容器:

#include "stdafx.h"
#include<iostream>
#include<vector> int main()
{
std::vector<int> arr;
arr.push_back();
arr.push_back(); for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << *it << std::endl;
} return ;
}

其中auto用到了C++11的类型推导。同时我们也可以使用std::for_each完成同样的功能:

#include "stdafx.h"
#include<algorithm>
#include<iostream>
#include<vector> void func(int n)
{
std::cout << n << std::endl;
} int main()
{
std::vector<int> arr;
arr.push_back();
arr.push_back(); std::for_each(arr.begin(), arr.end(), func); return ;
}

现在C++11的for循环有了一种新的用法:

#include "stdafx.h"
#include<iostream>
#include<vector> int main()
{
std::vector<int> arr;
arr.push_back();
arr.push_back(); for (auto n : arr)
{
std::cout << n << std::endl;
} return ;
}

上述方式是只读,如果需要修改arr里边的值,可以使用for(auto& n:arr),for循环的这种使用方式的内在实现实际上还是借助迭代器的,所以如果在循环的过程中对arr进行了增加和删除操作,那么程序将对出现意想不到的错误。

  其实这种用法在其他高级语言里早有实现,如php,Java,甚至是对C++进行封装的Qt,foreach也是有的。

原文地址:http://www.cnblogs.com/jiayayao/p/6138974.html

C++11之for循环的新用法《转》的更多相关文章

  1. C++11之for循环的新用法

    C++使用如下方法遍历一个容器: #include "stdafx.h" #include<iostream> #include<vector> int m ...

  2. C++11的for循环的新用法

    字符串 string str = "this is a string"; for(auto ch : str) cout << ch << endl; 等价 ...

  3. C++11常用特性介绍——for循环新用法

    一.for循环新用法——基于范围的for循环 for(元素类型 元素对象 : 容器对象) { //遍历 } 1)遍历字符串 std::string str = "hello world&qu ...

  4. Ext.Net学习笔记11:Ext.Net GridPanel的用法

    Ext.Net学习笔记11:Ext.Net GridPanel的用法 GridPanel是用来显示数据的表格,与ASP.NET中的GridView类似. GridPanel用法 直接看代码: < ...

  5. C++虚函数的新用法

    1.今天在segmentfault上看到了一个C++虚函数的新用法,先上代码 #include <iostream> using namespace std; class B { publ ...

  6. CSS选择器的新用法

    前面的话 现在,预处理器(如sass)似乎已经成为开发CSS的标配,正如几年前jQuery是开发JS的标配一样.JS的querySelector借鉴了jQuery的选择器思想,CSS选择器也借鉴了预处 ...

  7. js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 for循环

    js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 $(".progressName").each(f ...

  8. FastAdmin 使用 Git 更新的新用法 (2019-02-28)

    FastAdmin 使用 Git 更新的新用法 2019-02-28 新流程 增加一个 fastadmin 的远程仓库. 在项目的开发或主分支. 如果有代码更新将代码提交 commit. git pu ...

  9. 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创

    马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与   分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴 ...

随机推荐

  1. [转][C#]Environment 类

    当执行 Environment.GetEnvironmentVariables() 时,可以得到以下结果(受所安装软件影响,每台电脑都不一样) Count = ["SystemDrive&q ...

  2. CSS自定义滚动条样式

    原文地址:http://www.qianduan.net/css-custom-scroll-bar-style/ 相信很多人都遇到过在设计中自定义滚动条样式的情景,之前我都是努力说服设计师接受浏览器 ...

  3. error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ActionButton'.

    引用appcompat 类库提示 error: Error retrieving parent for item: No resource found that  matches the given ...

  4. Jmeter(二十八)Jmeter-Question之“HTTPS请求”

    前面在Jmeter-Question中有提到若干问题,有时间呢,我也会进行继续编写随笔,梳理自己的知识,本篇呢,便来记Jmeter发送https请求的过程 内容大致与http://blog.csdn. ...

  5. sqlserver 数据简单查询

    use StudentManageDB go select StudentName as 姓名,Gender as 性别,出生日期=birthday from Students where Gende ...

  6. 贪吃蛇 v1.01

    1.长度二节:2.如果触及屏幕边缘,游戏结束: 感谢张瑞阳同学改进 #include<bits/stdc++.h>#include<windows.h>#include< ...

  7. JSP基础解析

    EL表达式     https://www.cnblogs.com/zhouguanglin/p/8117406.html EL(Expression Language) 是为了使JSP写起来更加简单 ...

  8. [SQL]卸载数据库清理注册表方法regedit

    .打开注册表. 开始——运行——regedit——确定 .然后找到下面的文件夹,删除掉: HKEY_CURRENT_USER\ Software\ Microsoft\ Microsoft SQL S ...

  9. WordPress版微信小程序2.0版本发布

    利用业余时间对WordPress版微信小程序进行的升级,增加了一些功能,程序性能上做了一些优化.经过此次的版本升级,WordPress版微信小程序所需的基本功能已经具备. 开放源码地址:https:/ ...

  10. ubuntu 16.04在真实机安装后的静态ip的配置

    nssa-sensor1@nssa-sensor1:~$ vim /etc/network/interfaces 以下是编辑文件的内容# interfaces(5) file used by ifup ...