C++ range-based for循环

C++ range-based for循环

在这个主题中,我们将讨论C++编程语言中的range-based for循环。C++语言在C++11和后续版本中引入了一个新的概念,即range-based for循环,这比常规的For循环更好。range-based for循环不需要大量的编码来实现循环迭代。它是一个顺序迭代器,遍历容器中每个元素的范围(从开始到结束)。

C++ range-based for循环

语法

for (range_declaration : range_expression ) loop statement
  1. range_declaration: 用于声明一个变量,其类型与范围表达式表示的集合元素的类型相同或者引用了该类型。
  2. range_expression: 定义了一个表示适当元素序列的表达式。
  3. 循环语句: 定义了范围循环的循环体,包含一个或多个语句,这些语句将被重复执行直到范围表达式结束。

注意:如果我们不知道容器元素的数据类型,可以使用auto关键字自动识别范围表达式的数据类型。

使用范围循环打印数组的每个元素的程序

让我们以C++中使用范围循环打印int和double数组的示例为例。

program.cpp

#include 
using namespace std;
int main ()
{
int arr1 [5] = { 10, 20, 30, 40, 50};
double darr [5] = { 2.4, 4.5, 1.5, 3.5, 4.0 };

// use range based for loop
for ( const auto &var : arr1 )
{
cout << var << " " ;
}
// use auto keyword to automatically specify the data type of darr container. 
for ( const auto &var : darr )
{
cout << var << " " ;
}
return 0;
}

输出

10 20 30 40 50
2.4 4.5 1.5 3.5 4.0

展示基于范围的for循环中的向量的程序

让我们编写一个简单的程序来实现基于范围的for循环中的向量。

Program2.cpp

#include 
#include 
using namespace std;

int main()
{
    int x; // declare integer variable
    // declare vector variable
    vector  vect = {5, 10 , 25, 20, 25};

    // display vector elements
    for ( int x : vect)
    {
        cout << x << " ";
    }
    return 0;
}

输出

5 10 25 20 25

使用C++的范围基于for循环和引用来打印数组的程序

让我们来考虑一个使用C++的范围基于for循环来打印数组元素的示例。

Program3.cpp

#include 
#include 
 #include 
 using namespace std;

 int main(){
 array data = {1, 3, -2, 4, 6, 7, 9};
 cout << " Before updating the elements: " << endl;
 for (int x : data){
 cout << x << " ";
 }
 // pass the references
 for (int &itemRef : data){
 itemRef *= 3;
 }
 cout << endl << " After modification of the elements: " << endl;
 for (int x : data){
 cout << x << " ";
 }
 cout << endl;
 return 0;
 }

输出

Before updating the elements:
1 3 -2 4 6 7 9
 After modification of the elements:
3 9 -6 12 18 21 27

嵌套的基于范围的for循环

当一个循环在另一个循环的主体内定义时,该循环被称为嵌套的for循环。同样地,当我们在另一个基于范围的循环内定义一个范围时,这种技术被称为嵌套的基于范围的for循环。

语法:

for ( int x : range_expression) // outer loop
{
for ( int y : range_expression) // inner loop
{
// statement to be executed
}
// statement to be executed
}

在上面的语法中,我们在另一个循环中定义了一个基于范围的for循环。在这里我们在C++中调用了内部和外部的基于范围的for循环。

用C++打印嵌套的基于范围的for循环的程序

考虑一个示例来演示C++编程语言中的嵌套基于范围的for循环。

Range.cpp

#include 
using namespace std;
int main ()
{
int arr1[4]  = { 0, 1, 2, 3 };
int arr2[5] = { 1, 2, 3, 4, 5 };
// use nested range based for loop
for ( int x : arr1 )
{
// declare nested loop
for ( int y : arr2 )
{
cout << " x = " << x << " and j = " << y << endl;
}
}
return 0;
}

输出

x = 0 and j = 1
 x = 0 and j = 2
 x = 0 and j = 3
 x = 0 and j = 4
 x = 0 and j = 5
 x = 1 and j = 1
 x = 1 and j = 2
 x = 1 and j = 3
 x = 1 and j = 4
 x = 1 and j = 5
 x = 2 and j = 1
 x = 2 and j = 2
 x = 2 and j = 3
 x = 2 and j = 4
 x = 2 and j = 5
 x = 3 and j = 1
 x = 3 and j = 2
 x = 3 and j = 3
 x = 3 and j = 4
 x = 3 and j = 5

传统的for循环和基于范围的for循环有什么区别?

传统的for循环 被用于反复执行代码块,直到满足指定的条件为止。传统的for循环有三个参数,变量初始化,指定条件,和计数器,如果条件仍为真,则计数器加一。

语法:

for ( variable_initialization; specify_condition; updated_counter)
{
// statement to be executed;
}

range-based 循环

另一方面,我们在C++ 11及以后版本中有一个新的range-based for 循环可用。它有两个参数,范围声明和范围表达式。它也用于重复执行一段代码在一个范围内。

语法

for ( range_declaration : range_ expression )
{
loop _statement;
// statement to be executed;
} 

range_declaration用于声明与range_expression(容器)相关的变量类型。range_expression:它就像一个容器,以顺序方式保存相同类型的元素。loop_statement定义了在for循环内执行的语句。

range-based for循环的优点

  1. 容易使用,语法也简单。
  2. range-based for循环不需要计算容器中元素的数量。
  3. 它可以识别容器的起始和结束元素。
  4. 我们可以轻松修改容器的大小和元素。
  5. 它不会创建任何元素的副本。
  6. 它比传统的for循环更快。
  7. 它通常使用auto关键字来识别容器元素的数据类型。

range-based for循环的缺点

  1. 它无法遍历列表的一部分。
  2. 不能用于逆序遍历。
  3. 它无法在指针中使用。
  4. 它不提供当前元素的索引。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程