解析C++中的for循环以及基于范围的for语句使用
作者:goldensun 发布时间:2023-11-16 09:59:58
for循环语句
重复执行语句,直到条件变为 false。
语法
for ( init-expression ; cond-expression ; loop-expression )
statement;
备注
使用 for 语句可构建必须执行指定次数的循环。
for 语句包括三个可选部分,如下表所示。
for 循环元素
下面的示例将显示使用 for 语句的不同方法。
#include <iostream>
using namespace std;
int main() {
// The counter variable can be declared in the init-expression.
for (int i = 0; i < 2; i++ ){
cout << i;
}
// Output: 01
// The counter variable can be declared outside the for loop.
int i;
for (i = 0; i < 2; i++){
cout << i;
}
// Output: 01
// These for loops are the equivalent of a while loop.
i = 0;
while (i < 2){
cout << i++;
}
}
// Output: 012
init-expression 和 loop-expression 可以包含以逗号分隔的多个语句。例如:
#include <iostream>
using namespace std;
int main(){
int i, j;
for ( i = 5, j = 10 ; i + j < 20; i++, j++ ) {
cout << "i + j = " << (i + j) << '\n';
}
}
// Output:
i + j = 15
i + j = 17
i + j = 19
loop-expression 可以递增或递减,或通过其他方式修改。
#include <iostream>
using namespace std;
int main(){
for (int i = 10; i > 0; i--) {
cout << i << ' ';
}
// Output: 10 9 8 7 6 5 4 3 2 1
for (int i = 10; i < 20; i = i+2) {
cout << i << ' ';
}
// Output: 10 12 14 16 18
当 statement 中的 break、return 或 goto(转到 for 循环外部的标记语句)执行时,for 循环将终止。 for 循环中的 continue 语句仅终止当前迭代。
如果忽略 cond-expression,则认为其为 true,for 循环在 statement 中没有 break、return 或 goto 时不会终止。
虽然 for 语句的三个字段通常用于初始化、测试终止条件和递增,但并不限于这些用途。例如,下面的代码将打印数字 0 至 4。在这种情况下,statement 是 null 语句:
#include <iostream>
using namespace std;
int main()
{
int i;
for( i = 0; i < 5; cout << i << '\n', i++){
;
}
}
for 循环和 C++ 标准
C++ 标准中提到,for 循环中声明的变量将在 for 循环结束后超出范围。例如:
for (int i = 0 ; i < 5 ; i++) {
// do something
}
// i is now out of scope under /Za or /Zc:forScope
默认情况下,在 /Ze 下,for 循环中声明的变量在 for 循环的封闭范围终止前保持在范围内。
/Zc:forScope 无需指定 /Za 即可启用 for 循环中声明的变量的标准行为。
也可以使用 for 循环的范围差异,重新声明 /Ze 下的变量,如下所示:
// for_statement5.cpp
int main(){
int i = 0; // hidden by var with same name declared in for loop
for ( int i = 0 ; i < 3; i++ ) {}
for ( int i = 0 ; i < 3; i++ ) {}
}
这更类似于 for 循环中声明的变量的标准行为,后者要求 for 循环中声明的变量在循环完毕后超出范围。在 for 循环中声明变量后,编译器会在内部将其提升为 for 循环封闭范围中的局部变量,即使存在同名的局部变量也会如此。
基于范围的 for 语句
语句 statement 按顺序反复执行语句 expression 中的每个元素。
语法
for ( for-range-declaration : expression )
statement
备注
使用基于范围的 for 语句构造一个必须执行的循环范围,可以定义为任意一个循环访问,例如 std::vector,或者其他任意用 begin() 和 end()定义的范围。命名在 for-range-declaration 语句是属于 for 的,不能在 expression 或 statement中再次声明。请注意 自动 关键字是在 for-range-declaration 中部分语句的首选。
这段代码展示了如何使用 for 范围的循环来遍历数组和向量:
// 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[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 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 = 0; i < 10; ++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;
}
输出如下:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
end of integer array test
0.14159 1.14159 2.14159 3.14159 4.14159 5.14159 6.14159 7.14159 8.14159 9.14159
end of vector test
一个基于 for 循环终止于 statement 执行完成: break, return,或者 goto 转到一个语句外的 for 循环 continue 与语句终止当前 for 循环的迭代。
记住这些关于范围 for 的事实
自动识别数组。
识别那些有 .begin() 和 .end() 的容器。
使用基于自变量的查找 begin() 和 end() 。


猜你喜欢
- 本文实例为大家分享了C#实现飞行棋的具体代码,供大家参考,具体内容如下基于Winform框架写的不足之处请大佬指教using System;
- 本文实例为大家分享了Android实现滑动效果的具体代码,供大家参考,具体内容如下坐标系与视图坐标系相辅相成1、坐标系:描述了View在屏幕
- 给新建的winform程序添加资源文件夹Resources小菜鸟开始学习WinForm程序别人的项目都有资源文件夹放图片之类的,我的就是没有
- Android 获取IP地址最近做项目,有一个需求是Android设备获取当前IP的功能,经过一番查询资料解决了,记录下实现方法。1.使用W
- 简介本文介绍微服务架构中如何实现单点登录功能创建三个服务:操作redis集群的服务,用于多个服务之间共享数据统一认证中心服务,用于整个系统的
- C语言用结构体实现一个通讯录,通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址提供方法:1. 添加联系人
- #define Testusing System;namespace Wrox.ProCSharp.ParameterTestSample.
- 实现的功能比较简单,就是随机产生了四个字符然后输出。效果图如下,下面我会详细说一下实现这个功能用到了那些知识点,并且会把 这些知识点详细的介
- 1. 导入依赖包// retrofit, 基于Okhttp,考虑到项目中经常会用到retrofit,就导入这个了。 compil
- 某天一朋友突然发来一个地址,问我怎么获取这张图片的后缀名??将代码放在下面以供参考:using System;using System.Dr
- 本文实例总结了Java JDBC连接数据库常见操作。分享给大家供大家参考,具体如下:db.properties配置文件(MySql数据库)#
- 一、说明到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同
- TextView加载字体包在 Android 中,若需要使得某个TextView加载字体包,使用以下方式即可: Typeface typeF
- 概述:Flutter 标签类控件大全ChipFlutter内置了多个标签类控件,但本质上它们都是同一个控件,只不过是属性参数不同而已,在学习
- 我先说说这两种的方式的不同之处吧 第一种: 在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面 第二种: 在调
- 1、@Configuration&@Bean给容器中注册组件@Configuration及@Bean的使用参考如下代码:packag
- Spring Boot简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开
- 前言Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其
- 序、前言emmmmm,首先这篇文章讲的不是用BinaryFormatter来进行结构体的二进制转换,说真的BinaryFormatter这个
- 本文实例讲述了从C#程序中调用非受管DLLs的方法。分享给大家供大家参考。具体方法如下:前言:从所周知,.NET已经渐渐成为一种技术时尚,那