基于范围的for循环
语法:
for ( for-range-declaration : expression )
statement
注意一般用auto表达类型。不需要修改时常用引用类型
例子:
// range-based-for.cpp
// compile by using: cl /EHsc /nologo /W4
#include <iostream>
#include <vector>
using namespace std; int main()
{
// Basic 10-element integer array.
int x[] = { , , , , , , , , , }; // Range-based for loop to iterate through the array.
for( int y : x ) { // Access by value using a copy declared as a specific type.
// Not preferred.
cout << y << " ";
}
cout << endl; // The auto keyword causes type inference to be used. Preferred. for( auto y : x ) { // Copy of 'x', almost always undesirable
cout << y << " ";
}
cout << endl; for( auto &y : x ) { // Type inference by reference.
// Observes and/or modifies in-place. Preferred when modify is needed.
cout << y << " ";
}
cout << endl; for( const auto &y : x ) { // Type inference by reference.
// Observes in-place. Preferred when no modify is needed.
cout << y << " ";
}
cout << endl;
cout << "end of integer array test" << endl;
cout << endl; // Create a vector object that contains 10 elements.
vector<double> v;
for (int i = ; i < ; ++i) {
v.push_back(i + 0.14159);
} // Range-based for loop to iterate through the vector, observing in-place.
for( const auto &j : v ) {
cout << j << " ";
}
cout << endl;
cout << "end of vector test" << endl;
}
基于范围的for循环的更多相关文章
- C++11基于范围的for循环
C++11包含一种新的 for 循环,称为基于范围的 for 循环,可以简化对数组元素的遍历.格式如下: for(Type VarName : Array){ //每个元素的值会依次赋给 VarNam ...
- Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 循环队列
Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...
- C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)
3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...
- 基于范围的for循环(STL)
1. ]={4.99,5.99,6.99,7.99,8.99}; for (double x : prices) cout<<x<<endl; //////////////// ...
- 基于范围的for循环(C++11)
C++11新增了一种循环:基于范围的for循环.这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,如下例所示 for语句允许简单的范围迭代:(只遍历, ...
- 第9课 基于范围的for循环
1. 基于范围的for循环(range-based for) (1)语法:for(decl : coll){//statement} ①decl用于声明元素及类型,如int elem或auto ele ...
- c++11 基于范围的for循环
c++11 基于范围的for循环 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> # ...
- c++11——基于范围的for循环
c++11中有基于范围的for循环,基于范围的for循环可以不再关心迭代器的概念,只需要关系容器中的元素类型即可,同时也不必显式的给出容器的开头和结尾. int arr[] = {1, 2, 3, 4 ...
- c++11之一: 基于范围的for循环
#include <iostream> using namespace std; int main(){ ]{,,,,}; for (int& e: ary) e *= ; for ...
- C++11特性中基于范围的for循环
本文摘录柳神笔记: 除了像C语⾔的for语句 for (i = 0; i < arr.size(); i++) 这样,C++11标准还为C++添加了⼀种新的 for 循环⽅ 式,叫做基于范围 ...
随机推荐
- 《Spring实战》 1-2
第1章 Spring之旅 Spring容器 Spring中bean的生命周期 Spring框架中的模块 Spring Portfolio 第2章 装配Bean 自动化装配bean 通过Java代码装配 ...
- 如何利用mount命令挂载另一台服务器上的目录
文件服务器(被挂载机):192.168.1.100 操作机(挂载到机):192.168.1.200 也就是说,你在操作机上进行的操作,实际上都到文件服务器上去了: 1. 开启NFS服务: 在文件服务器 ...
- zookeeper日常报错总结
1:创建子节点的时候 没有根节点 org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for ...
- Eltwise层解析
Concat层虽然利用到了上下文的语义信息,但仅仅是将其拼接起来,之所以能起到效果,在于它在不增加算法复杂度的情形下增加了channel数目.那有没有直接关联上下文的语义信息呢?答案是Eltwise层 ...
- shell操作典型案例--FTP操作
从FTP服务器上下载文件或上传文件到FTP服务器是生产环境中比较常见的场景之一. shell操作FTP的方式整理如下: 思路一:使用shell调用ftp等客户端 使用FTP方式,通过shell调用ft ...
- 如何开启一个Django项目
一:新建的Django工程 新建了一个Django工程后,工程会自动创建有两个templates文件夹和unitled文件夹,再加上一个manage.py文件. 二:Django开发的一般流程 在工程 ...
- ORB_SLAM2 源码阅读 ORB_SLAM2::ORBextractor
整体架构 构造函数进行初始化,传入设定几个重要的成员变量.nfeatures(特征点的个数).nlevels(构造金字塔的层数).scaleFactor(金字塔中相邻层图像的比例系数).iniThFA ...
- JavaScript验证注册信息
<script language="javascript"> function check_login(form){ if(form.username.value==& ...
- MySQL 5.7以后怎么查看索引使用情况?
MySQL 5.7以后怎么查看索引使用情况? 0.在sys库中查看没用的索引 root@localhost [sys]>select * from schema_unused_indexes; ...
- ps查看CPU和内存占用前10的进程
内存增序 ps aux --sort rss 内存减序 ps aux --sort -rss cpu增序 ps auxw --sort=%cpu cpu减序 ps auxw --sort=-%cpu ...