解析C++中的for循环以及基于范围的for语句使用

所属分类: 软件编程 / C 语言 阅读数: 75
收藏 0 赞 0 分享

for循环语句

重复执行语句,直到条件变为 false。

语法

for ( init-expression ; cond-expression ; loop-expression ) 
  statement;

备注
使用 for 语句可构建必须执行指定次数的循环。
for 语句包括三个可选部分,如下表所示。
for 循环元素

2016116165838737.png (878×231)

下面的示例将显示使用 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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多