-
范围for语句
2017-08-02 14:33:24范围for语句C++11新标准引入了一种更简单的 for 语句,这种语句可以遍历容器或其他序列的所有元素。范围 for 语句的语法形式是: for (declaration : expression) statement expression 表示的必须是一个序列,...范围for语句
C++11新标准引入了一种更简单的 for 语句,这种语句可以遍历容器或其他序列的所有元素。范围 for 语句 的语法形式是:
for (declaration : expression)
statement
expression 表示的必须是一个序列,比如用花括号括起来的初始值列表、数组或者 vector 或 string 等类型的对象,这些类型的共同特点是拥有能返回迭代器的 begin 和 end 成员。
declaration 定义一个变量,序列中的每个元素都得能转换成该变量的类型。确保类型相容的最简单的办法是使用 auto 类型说明符,这个关键字可以令编辑器帮助我们指定合适的类型。如果需要对序列中的元素执行写操作,循环变量必须声明成 引用类型。
每次迭代都会重新定义循环控制变量,并将其初始化成序列中的下一个值,之后才会执行statement 。像往常一样,statement 可以是一条单独的语句也可以是一个块。所有元素都处理完毕后循环终止。
e.g. 将 vector 对象中的每个元素都翻倍:vector<int> v = {0,1,2,3,4,5,6,7,8,9}; // 范围变量必须是引用类型,这样才能对元素执行写操作 for(auto &r : v) // 对于v中的每一个元素 r *= 2; // 将v中每个元素的值都翻倍
for 语句头声明了循环控制变量r,并把它和v关联在一起,我们使用关键字 auto 令编译器为 r 指定正确的类型。由于准备修改 v 的元素的值,因此将 r 声明成引用类型。此时,在循环体内给 r 赋值,即改变了 r 所绑定的元素的值。
范围 for 语句的定义来源于与之等价的传统 for 语句:for(auto beg = v.begin(), end = v.end(); beg != end; ++beg){ auto &r = *beg; // r必须是引用类型,这样才能对元素执行写操作 r *= 2; // 将v中每个元素的值翻倍
-
范围 for 语句
2020-06-13 14:57:47C++11 新标准引入了范围for语句,用来方便地遍历一个容器或其他序列。其语法形式是: for(declaration : expression) statement expression表示必须是一个序列,比如用花括号括起来的初始值列表、数组...C++11 新标准引入了范围for语句,用来方便地遍历一个容器或其他序列。其语法形式是:
for(declaration : expression) statement
expression表示必须是一个序列,比如用花括号括起来的初始值列表、数组、或者 vector 或 string 等类型的对象。这些类型的共同特点是拥有能够返回迭代器的 begin 和 end 成员。
declaration定义一个变量,auto 确保类型相容,让编译器帮我们找到合适的类型。如果需要进行修改的操作,需用声明称引用类型,因为默认是按值传递。vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (auto &r : v) { r*= 2; // v 中的所有值都翻倍 } //与之等价的传统 for 语句 for (auto beg = v.begin(); beg != v.end(); ++beg) { auto &r = *beg; // r 必须为引用类型, * 2 的写操作才有效 r *= 2; }
看到上面的等价传统 for 语句,就能够明白为什么在循环体中不能对 vec 进行增删操作,并且如果要写操作,需要声明引用。
还有一点需要强调,在使用范围 for 语句遍历多维数组时,也必须声明为引用类型,不然编译通过不了,因为 auto 默认推断的数组元素是指向数组首元素指针。int n[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (auto &row : n) // row 必须是引用,才能保证 row 是 int[] , 而不是 int* for (auto &col : row) cout << col << endl;
-
范围For 语句
2018-06-05 21:43:13For (declaration: expression) StatementDeclaration:定义一个作为访问方式的变量。Expression:指向需要操作的对象。Statement:需要执行的操作。for(int& i : vec) //若需要修改对象中的值则要加引用...For (declaration: expression) Statement
Declaration:定义一个作为访问方式的变量。
Expression:指向需要操作的对象。
Statement:需要执行的操作。
for(int& i : vec) //若需要修改对象中的值则要加引用(&) { i++; } for (auto i : vec)//推荐使用auto类型说明符 { cout << i << endl; // 输出更新后的数值 }
注:expression为一个“序列”,可进行自定义,需要对象满足一下条件:
指针或迭代器(*);
拥有能够返回迭代器的begin和end成员函数;
前缀自加操作符(++);
不等关系运算符(!=);
// a simple iterator sample. #include <iostream> using namespace std; // forward-declaration to allow use in Iter class IntVector; class Iter { private: int _pos; const IntVector *_p_vec; public: Iter(const IntVector* p_vec, int pos) : _pos(pos), _p_vec(p_vec){} // these three methods form the basis of an iterator for use with a range-based for loop bool operator!=(const Iter& other) const { return _pos != other._pos; } // this method must be defined after the definition of IntVector since it needs to use it int operator*() const; const Iter& operator++() { ++_pos; // although not strictly necessary for a range-based for loop // following the normal convention of returning a value from // operator++ is a good idea. return *this; } }; class IntVector { private: int _data[100]; public: IntVector(){} int get(int col) const { return _data[col]; } Iter begin() const { return Iter(this, 0); } Iter end() const { return Iter(this, 100); } void set(int index, int val) { _data[index] = val; } }; int Iter::operator*() const { return _p_vec->get(_pos); } // sample usage of the range-based for loop on IntVector int main() { IntVector v; for(int i = 0; i < 100; i++) { v.set(i,i); } for( int i : v) { cout << i << endl; } }
-
C++ 范围for语句
2018-11-08 21:54:01范围for语句在C++11中间,引入了范围for语句,它的作用就是简化遍历给定序列的操作。
语法形式如下:
for (declaration : expression)
statementdeclaration: 定义的变量
expression: 要遍历的对象
statement: 语句举例:
string s = "string"; //一般打印字符串s的每个字母的方法 for(int i = 0; i < s.size(); i++) { cout << s[i] << " "; } //范围for语句 for(auto c : s) { cout << c << " "; }
是不是很简洁?
范围for语句更多的用在容器的遍历:
vector<int> v = {1,2,3}; //一般遍历 for(auto i = v.cbegin(); i != v.cend(); i++) { cout << *i << " "; } //范围for语句 for(auto i : v) { cout << i << " "; //注意,没有*哦 }
-
C++ string和范围for语句
2020-01-22 15:57:47初始化 //string 类型的初始化方法 string s1; string s2 = s1; string s3 = "lol"; string s4("JarvenIV"); string s5(7,'7'); //连续n个字符组成的串 ...//c++范围for语句,处理字符串中的每个字符 /... -
范围for语句的整理
2019-10-13 16:45:23范围for语句的整理 2018-03-11 20:45:32Enterprise_阅读数 549更多 分类专栏:C++基础 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。 本文链接:... -
C++之for循环、范围for语句探究
2020-12-26 01:31:02C++新标准提供的范围for语句.这种语句遍历给定序列中个元素并对序列中每一个值执行某种操作,其语法形式是: 其中,expression部分是一个对象,用于表示一个序列。declaration部分负责定义一个变量,该变量... -
C++11--范围for语句
2017-12-17 15:17:37从编译器的角度去理解范围for语句的执行过程;实际上就是把范围for语句转换成了传统的for循环语句。 下面的两种执行过程完全等价,只是采用了不同的标准函数。//范围for循环 for (decl : coll) { statement } //... -
C++ 容器与范围for 语句
2020-11-17 23:00:17C++ 容器与范围for 语句 #预定义 #include using std::vector #容器是一个模版,需要用具体的类型进行实例化 vector ivec vector name vector <my_class> my_own_object 可以默认初始化vector,从而创建一个... -
C-的string类型及范围for语句
2020-04-29 16:02:11title date tags categories description ... C++的string类型及范围for语句 2020-04-27 14:35:19 -0700 string 范围for语句 C/C++ 标准库string... -
C++11 范围for语句
2017-03-09 09:28:54C++11提供了范围for语句,用来方便地遍历一个容器。 其语法形式是:for(declaration : expression) statement代码示例如下:vector<int> v={0,1,2,3,4,5,6,7}; //若想改变容器内元素的值,范围变量必须是**引用**...
-
Spark Graphx Pregel(pregel参数详解,pregel调用实现过程的详细解释)
-
C#怎么才能让comboBox控件失去焦点
-
Excel高级图表技巧
-
主成分分析.pptx
-
【数据分析-随到随学】数据可视化
-
Duplicate Bug Report Detection Using Dual-Channel Convolution Neural Networks
-
java数据类型:byte
-
开启 Python 爬虫之路 必知必会的知识
-
linux网络命令
-
【2021】UI自动化测试框架(Selenium3)
-
Python入门到项目直通车
-
企业搭建私域流量的正确打开方式
-
转行做IT-第2章 HTML入门及高级应用
-
2021最新Kubernetes(k8s)集群实战精讲
-
2021-01-23
-
【数据分析-随到随学】数据分析基础及方法论
-
小白变叫兽.docx
-
算法导论二(排序和顺序统计量)——编程大牛的必经之路
-
云计算基础-Linux系统管理员
-
Microsoft Remote Desktop For ec2-18-163-8-46.ap-east-1.compute.amazonaws.com.rdp