一些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 中的一个新语法,允许定义匿名函数.匿名 ...
随机推荐
- XamarinForms教程构建XamarinForms开发环境
构建XamarinForms开发环境 所谓Xamarin.Forms的开发环境,就是指在基本硬件和数字软件的基础上,为支持系统软件和应用软件的工程化开发和维护而使用的一组软件,简称SDE.对于任何的程 ...
- PHP 笔记——基础
一.PHP 简介 1. PHP是什么 PHP:Hypertext Preprocessor,即超文本预处理器. PHP是一种跨平台.服务器端.可嵌入HTML文件的脚本语言. 嵌入了PHP代码的HTML ...
- 【期望DP】BZOJ4008- [HNOI2015]亚瑟王
题目大意 有\(n\)张卡牌,\(r\)轮游戏.每张卡牌只能用至多一次,每张卡牌被用到的概率为\(p_i\).现在从左往右轮,直到最右一张卡片或者某张卡片被用到.如果某张卡牌被用到,产生\(d_i\) ...
- padding Oracle attack(填充Oracle攻击)
最近学习到一种老式的漏洞,一种基于填充字节的漏洞.就想记录下来,早在2010年的blackhat大会上,就介绍了padding Oracle漏洞,并公布了ASP.NET存在该漏洞.2011年又被评选为 ...
- Android之Fragment优点
什么是Fragment 自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片.片段.其目的是为了解决不同屏幕分辩率的动态和灵活UI设计.大屏幕如平板小屏幕如手机,平 ...
- WebSQL的基本使用过程
1.创建或打开数据库(openDatabase) var db = openDatabase('dbname', '1.0', 'discription', 2 * 1024); // 目前测试只有C ...
- wampserver -- 解决Exception Exception in module wampmanager.exe at 000F15A0
Learn from: http://hi.baidu.com/spt_form/item/4b4533476c3b92a6de2a9f78 系统:windows2003 32bit wampserv ...
- 【原创】MySQL+MyEclipse+对象映射文件,schema与category的关系
(一) 1.映射文件的类如下写法:class name="com.sanqing.po.SysUser" table="sys_user" catalog=& ...
- URAL 1995 Illegal spices 贪心构造
Illegal spices 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=1995 Description Jabba: Han, m ...
- Druid如何自动根据URL自动识别DriverClass的
Druid是根据url前缀来识别DriverClass的,这样使得配置更方便简洁. 前缀 DriverCLass 描述信息 jdbc:odps com.aliyun.odps.jdbc.OdpsDri ...