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++作为一门软件开发语言,它的流行度是在减少的.主要原因在于语言的复杂和灵活导致软件开发成本提高,这体现在开发周期和 ...
随机推荐
- VMware Workstation 10.0.4.2249910 CN
VMware Workstation 10.0.4.2249910.exe Workstation10.0.4修复了微软Windows 8.1和Windows Server 2012操作系统中的内存问 ...
- Similarity-based Learning
Similarity-based approaches to machine learning come from the idea that the best way to make a predi ...
- Android Listview & Adapter
Listview主要有两个职责: 将数据填充到布局 处理用户的选择点击等操作 列表的显示需要三个元素: ListVeiw 用来展示列表的View 适配器(Adapter) 用来把数据映射到ListVi ...
- hive的Query和Insert,Group by,Aggregations(聚合)操作
1.Query (1)分区查询 在查询的过程中,采用那个分区来查询是通过系统自动的决定,但是必须是在分区列上基于where子查询. SELECT page_views.* FROM page_view ...
- Java中的HashSet和TreeSet
1:Set集合(理解) (1)Set集合的特点 无序,唯一 (2)HashSet集合(掌握) A:底层数据结构是哈希表(是一个元素为链表的数组) B:哈希表底层依赖两个方法:hashCode()和eq ...
- BZOJ 3110 树套树 && 永久化标记
感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...
- ExpandableListView的OnitemLongclickListener事件
expandableListView是带分组的Listview,通常会有setOnChildClickListener,setOnGroupClickListener,但如果是长按的事件,可以用以下方 ...
- oracle日期操作
日期操作:ADD_MONTHS(date,i) 作用 返回在自定日期上添加的月份 i是整数 如果i是小数,则截取整数部分 i是负数 原有日期减去相应部分 例子: SQL> select add_ ...
- PHP、JAVA、C#、Object-C 通用的DES加密
PHP.JAVA.C#.Object-C 通用的DES加密 PHP: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- maven的SNAPSHOT版本和正式版本不同
转载文章: http://www.huangbowen.net/blog/2016/01/29/understand-official-version-and-snapshot-version-in- ...