Range-Based for Loops
for ( decl : coll )
{
statement
}
where decl is the declaration of each element of the passed collection coll and for which the statements specified are called.
. using the initializer list
for ( int i : { , , , , , , , } )
{
std::cout << i << std::endl;
} . normal container
std::vector<double> vec;
...
for ( auto& elem : vec )
{
elem *= ;
}
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.
To avoid calling the copy constructor and the destructor for each element, you should usually declare the current element to be a constant reference.
template <typename T>
void printElements (const T& coll)
{
for (const auto& elem : coll)
{
std::cout << elem << std::endl;
}
} //which is equivalent to the following:
{
for (auto _pos=coll.begin(); _pos != coll.end(); ++_pos )
{
const auto& elem = *_pos;
std::cout << elem << std::endl;
}
}
Which violate the rules introduced in "the philosophy behind of the design of the STL"
//perfectly correct one
template<T>
printElements(iterator<T> begin, iterator<T> end)
{
for(;begin < end && begin != end; begin ++)
{
const auto& element = *begin;
std::cout << element << std::endl;
}
}
Range-Based for Loops的更多相关文章
- boost range
1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...
- C++ 11标准
C++11,也称为C++0x.为目前C++编程语言的最新正式标准(ISO/IEC 14882:2011).它将取代第二版标准ISO/IEC 14882:2003(第一版ISO/IEC 14882:19 ...
- vtkMapper
本文只是整理了该网页的内容:http://www.cnblogs.com/lizhengjin/archive/2009/08/16/1547340.html vtkMapper是一个抽象类,指定了几 ...
- 快速入门系列--NOSQL--07MongoDB
从我第一次听到Nosql这个概念到如今已经走过4个年头了,但仍然没有具体的去做过相应的实践.最近获得一段学习休息时间,购买了Nosql技术实践一书,正在慢慢的学习.在主流观点中,Nosql大体分为4类 ...
- [译]学习IPython进行交互式计算和数据可视化(六)
第五章:高性能并行计算 一个反复被提及的反对使用Python进行高性能数值计算的言论是这种语言是动态解释型的,速度太慢.一种编译型低级语言,如C,能提供比它快几个数量级的运算速度.我们在第三章--使用 ...
- [译]学习IPython进行交互式计算和数据可视化(二)
第一章:开始使用IPython 在本章中,我们首先进行一遍IPython的安装过程,在大概浏览一下IPython提供的功能.IPython提供了一个高度优化的Python控制台和Notebook.除此 ...
- Python性能优化的20条建议 (转载)
优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(1).不同的场 ...
- C++类型引用浅析
C++类型引用浅析 引言 从最早被Bjarne Stroustrup 发明,作为C语言的扩展,到广为人知C++98标准,再到最新的C++11.C++14和C++17标准,C++一直在不断地进步.演化. ...
- c++11介绍
C++11标准是 ISO/IEC 14882:2011 - Information technology -- Programming languages -- C++ 的简称[1] . C++11 ...
- C++进阶引导
1.C++的用途和意义 t0185b047e29feffc26.jpg 总体来说,C++作为一门软件开发语言,它的流行度是在减少的.主要原因在于语言的复杂和灵活导致软件开发成本提高,这体现在开发周期和 ...
随机推荐
- c语言 四种方法调用数组
#include <stdio.h> /********************************* * 方法1: 第一维的长度可以不指定 * * 但必须指定第二维的长度 * * ...
- 观看github前100开源项目的读后感
文章来自:http://www.oschina.net/news/61416/github-top-100-objective-c-projects?from=20150412 ReactiveCoc ...
- Ubuntu安装gfortran
命令行运行 sudo apt-get install gfortran
- Map/Reduce个人实战--生成数据测试集
背景: 在大数据领域, 由于各方面的原因. 有时需要自己来生成测试数据集, 由于测试数据集较大, 因此采用Map/Reduce的方式去生成. 在这小编(mumuxinfei)结合自身的一些实战经历, ...
- LintCode Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...
- Directory的GetFiles方法
想实现一个功能 :比如多个业务审批流程公用一个审批表的时候,有一个提示审批信息的页面 ,点击该页面不同的业务审批流程记录的时候,跳转到不同业务流程的详细显示界面 额 这样说 貌似以后我自己也看不明白 ...
- XListView刷新
package com.example.da; import java.util.ArrayList;import java.util.List; import com.badu.net.Networ ...
- C语言实现Web客户端(转-kungstriving)
和我的上一篇文章是一起写的,呵呵,大家给提点意见啊. :-) /*********filename : Client.cpp**************** 该程序通过标准socket实 ...
- pull方法解析xml
private void getData() { new Thread() { public void run() { try { ...
- 《Visual C++并行编程实战》译者序
说来凑巧,当开始着手这本书的翻译时,我刚刚入手了自己第一台四核计算机,而翻译工作临近完成之时,我又为自己添置了一台iPad 2(这是一台双核计算机).由此可见,多核计算机已经完全进入了我的日常生活.鉴 ...