一些C++11语言新特性 - Range-Based for Loops
1. Range-Based for Loops
for ( decl : coll ) {
statement
}
eg:
for ( int i : { , , , , , , , } ) {
std::cout << i << std::endl;
}
std::vector<double> vec;
...
for ( auto& elem : vec ) {
elem *= ;
}
Here, declaring elem as a reference is important because otherwise the statements in the body of the for loop act on a local copy of the elements in the vector (which sometimes also might be useful).
This means that to avoid calling the copy constructor and the destructor for each element, you should usually declare the current element to be a constant reference. Thus, a generic function to print all elements of a collection should be implemented as follows:
template <typename T>
void printElements (const T& coll)
{
for (const auto& elem : coll) {
std::cout << elem << std::endl;
}
}
那段range-based for loops代码等价于如下:
for (auto _pos=coll.begin(); _pos != coll.end(); ++_pos ) {
const auto& elem = *_pos;
std::cout << elem << std::endl;
}
int array[] = { , , , , };
long sum=; // process sum of all elements
for (int x : array) {
sum += x;
}
for (auto elem : { sum, sum*, sum* } ) { // print 15 30 60
std::cout << elem << std::endl;
}
一些C++11语言新特性 - Range-Based for Loops的更多相关文章
- 一些C++11语言新特性 - Uniform Initialization
1. Uniform Initialization , , }; std::vector<, , , , , , }; std::vector<std::string> cities ...
- C++11新特性——range for
很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...
- 【C++11】新特性——Lambda函数
本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...
- C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...
- 【Qt开发】Qt5 中对 C++11 一些新特性的封装
C++11 是现在的 C++ 标准的名称,C++11 为 C++ 语言带来很多新特性. 而 Qt 4.8 是 Qt 首个在其 API 中开始使用一些新的 C++11 特性的版本,我之前写过一篇博文:C ...
- C# 11 的新特性和改进前瞻
前言 .NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定.在这个时间点上,不少新特性都已经实现完毕并合并入主分支 C# 11 包含的新特性和改进非常多,类型系统相比 ...
- 【C++11】新特性——auto的使用
[C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...
- atitit.Oracle 9 10 11 12新特性attilax总结
atitit.Oracle 9 10 11 12新特性 1. ORACLE 11G新特性 1 1.1. oracle11G新特性 1 1.2. 审计 1 1.3. 1. 审计简介 1 1.4. ...
- Qt5 中对 C++11 一些新特性的封装
在 Qt5 中,提供更多 C++11 的特性支持,接下来我们将进行详细的说明. slots (槽) 的 Lambda 表达式 Lambda表达式 是 C++11 中的一个新语法,允许定义匿名函数.匿名 ...
随机推荐
- AOP的自动代理
Spring的aop机制提供两类方式实现类代理.一种是单个代理,一种是自动代理. 单个代理通过ProxyFactoryBean来实现(就如上面的配置). 自动代理:自动代理能够让切面定义来决定那个be ...
- Arduino可穿戴教程之第一个程序——连接硬件选择板子(二)
Arduino可穿戴教程之第一个程序——连接硬件选择板子(二) 2.4.2 连接硬件 在选择好示例程序后就可以将LilyPad通过LilyPad编程器连接到电脑了. 2.4.3 选择板子 如果你了 ...
- 【BZOJ 3218】 3218: a + b Problem(最小割+可持久化线段树)
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1440 Solved: 545 Description Inp ...
- 苹果无sim卡激活
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 苹果无sim卡激活 这是不行的... 必须有SIM 卡. 有可能卡贴坏了,更换卡贴.
- NTFS ADS带来的web安全问题
有关ADS的简单说明请看http://www.xfocus.net/articles/200212/466.html 可以看到ADS在很久以前就被一些安全人员所关注,并且也提出了一些经典的利用,比如隐 ...
- lightoj 1296 - Again Stone Game 博弈论
思路:由于数据很大,先通过打表找规律可以知道, 当n为偶数的时候其SG值为n/2; 当n为奇数的时候一直除2,直到为偶数m,则SG值为m/2; 代码如下: #include<stdio.h> ...
- CentOS 6.9下的iptables在本机用DNAT转发指定IP到内网IP无效的问题解决(127.0.0.1)
比如: iptables -t nat -A OUTPUT -p tcp -d 192.168.1.0/24 --dport 2222 -j DNAT --to-destination 127.0.0 ...
- Linux下使用SSH远程执行命令方法收集
说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...
- 我的jlink破解失败经历
http://fallenwind.spaces.eepw.com.cn/articles/article/item/59116 标题:我的jlink破解失败经历2009-07-12 01:16:56 ...
- How to make a combo box with fulltext search autocomplete support?
I would like a user to be able to type in the second or third word from a TComboBoxitem and for that ...