一些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 中的一个新语法,允许定义匿名函数.匿名 ...
随机推荐
- XPath中的text()和string()区别(转)
原文地址 : http://blog.csdn.net/jiangchao858/article/details/63314426 本质区别 text()是一个node test,而string()是 ...
- HDU 6134 Battlestation Operational(莫比乌斯反演)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6134 [题目大意] 求$\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\ ...
- 执行时间 Exercise07_16
import java.util.Arrays; import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:执行时间 * */ pu ...
- 初涉springboot(二)
概述 1.环境说明 2.HelloWorld项目细节 3.小结 一. 关于环境补充 在上一篇的HellWorld项目中,我们构建SpringBoot项目,采用的是jdk1.8版本,springbo ...
- bzoj 1211: [HNOI2004]树的计数 -- purfer序列
1211: [HNOI2004]树的计数 Time Limit: 10 Sec Memory Limit: 162 MB Description 一个有n个结点的树,设它的结点分别为v1, v2, ...
- asp.net mvc中DropDownList
asp.net mvc中DropDownList的使用. 下拉列表框 以分为两个部分组成:下拉列表和默认选项 DropDownList扩展方法的各个重载版本基本上都会传递到这个方法上: publi ...
- Nginx反向代理 实现Web负载均衡
实现负载均衡的方式有很多种,DNS.反向代理.LVS负载均衡器(软件实现).F5(负载均衡器,硬件,非常昂贵)这里我们只提到基于DNS,以及反向代理的方式来实现负载均衡Web服务 DNS服 ...
- 开发Google Material Design风格的WPF程序
今天在网上看到了一个Material Design风格的WPF皮肤,看上去还是挺不错的 这个项目是开源的,感兴趣的朋友可以下载试下: https://github.com/ButchersBoy/Ma ...
- It's a Buck; It's a Boost, No! It's a Switcher!
It's a Buck; It's a Boost, No! It's a Switcher! Sanjaya Maniktala, National Semiconductor Corp., San ...
- SMT Surface Mount Technology footprint references
http://en.wikipedia.org/wiki/Surface-mount_technology Surface-mount technology (SMT) is a method for ...