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的更多相关文章

  1. boost range

    1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...

  2. C++ 11标准

    C++11,也称为C++0x.为目前C++编程语言的最新正式标准(ISO/IEC 14882:2011).它将取代第二版标准ISO/IEC 14882:2003(第一版ISO/IEC 14882:19 ...

  3. vtkMapper

    本文只是整理了该网页的内容:http://www.cnblogs.com/lizhengjin/archive/2009/08/16/1547340.html vtkMapper是一个抽象类,指定了几 ...

  4. 快速入门系列--NOSQL--07MongoDB

    从我第一次听到Nosql这个概念到如今已经走过4个年头了,但仍然没有具体的去做过相应的实践.最近获得一段学习休息时间,购买了Nosql技术实践一书,正在慢慢的学习.在主流观点中,Nosql大体分为4类 ...

  5. [译]学习IPython进行交互式计算和数据可视化(六)

    第五章:高性能并行计算 一个反复被提及的反对使用Python进行高性能数值计算的言论是这种语言是动态解释型的,速度太慢.一种编译型低级语言,如C,能提供比它快几个数量级的运算速度.我们在第三章--使用 ...

  6. [译]学习IPython进行交互式计算和数据可视化(二)

    第一章:开始使用IPython 在本章中,我们首先进行一遍IPython的安装过程,在大概浏览一下IPython提供的功能.IPython提供了一个高度优化的Python控制台和Notebook.除此 ...

  7. Python性能优化的20条建议 (转载)

    优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(1).不同的场 ...

  8. C++类型引用浅析

    C++类型引用浅析 引言 从最早被Bjarne Stroustrup 发明,作为C语言的扩展,到广为人知C++98标准,再到最新的C++11.C++14和C++17标准,C++一直在不断地进步.演化. ...

  9. c++11介绍

    C++11标准是 ISO/IEC 14882:2011 - Information technology -- Programming languages -- C++ 的简称[1]  . C++11 ...

  10. C++进阶引导

    1.C++的用途和意义 t0185b047e29feffc26.jpg 总体来说,C++作为一门软件开发语言,它的流行度是在减少的.主要原因在于语言的复杂和灵活导致软件开发成本提高,这体现在开发周期和 ...

随机推荐

  1. mysq常见问题

    1.Reading table information for completion of table and column names You can turn off this feature t ...

  2. centos lamp

    一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...

  3. reverse-daily(1)-audio_visual_receiver_code

    本人第一篇随笔,就以一篇CTF逆向分析的文章开始吧! 链接:http://pan.baidu.com/s/1eS6xFIa 密码:u14d 因为re的分析比较琐碎,所以主要就挑一些重点东西来说. 据说 ...

  4. Win10/UWP新特性系列—使用打印机

    微软在Win10时代终于完成的设备系统的大统一,"56个民族,56支花……"(⊙o⊙)…,既然统一了,那么也就意味着API也统一了,所以在UWP中,我们就可以使用统一的打印API来 ...

  5. Android 中Thread,Handler,Loop学习

    1.先看一下最简单的进度条示例 EG: package com.sxz.android.thread; import java.util.concurrent.atomic.AtomicBoolean ...

  6. 解决 xx.h has been modified since the precompiled header 系统头文件被修改

    fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev ...

  7. 在 Mac OS X Yosemite 10.10.5 上配置 OpenGL 编程环境

    这个教程主要参考了youtube上的视频 Getting Started in OpenGL with GLFW/GLEW in Xcode 6 ,这个视频有点问题,不能照搬.本人通过自己摸(瞎)索( ...

  8. Jena TDB assembler syntax

    1 introduction Assembler is a DSL of Jena to specify something to build, models and dataset, for exa ...

  9. document的一点点

    var str = "hello world" document.write(str.length)输出字符串长度   12 给string 添加样式 <p>Big: ...

  10. phpwind之关闭账号通

    phpwind的账号通功能早就失效了,但是首页的链接一直存在,造成了很不好的影响 但是后台打开账号通功能又打不开,所以想到了在前端的模板中通过屏蔽这部分代码的方法隐藏掉这个功能在首页的显示 1.打开/ ...