[C++] range-based for loop (C++11)


First, the basic syntax of range for

In C++98, if you want to traverse an array, you can do it in the following way:

#include<iostream>
using namespace std;
void TestFor()
{
    
    
	int array[] = {
    
     1, 2, 3, 4, 5 };
	for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
		cout << array[i] << " ";
	cout << endl;
}
int main()
{
    
    
	TestFor();
	return 0;
}

insert image description here
For a ranged collection, it is redundant and sometimes error-prone for the programmer to specify the range of the loop. Therefore, range-based for loops were introduced in C++11.
The parentheses after the for loop are divided into two parts by the colon ":": the first part is the variable used for iteration in the range, and the second part indicates the range to be iterated.

Look at the definition of the range for statement:

for (declaration : expression)
  statement

Among them, the expression part is a sequence, and the declaration part is responsible for defining a variable that will be used to access the basic elements in the sequence.
code example

#include<iostream>
using namespace std;
void TestFor2()
{
    
    
	int array[] = {
    
     1, 2, 3, 4, 5 };
	//定义一个变量 e 然后每次都从array取数据赋值给e,自动判断结束
	for (auto e : array)
		e *= 2;
	for (auto e : array)
		cout << e << " ";
}
int main()
{
    
    
	TestFor2();
	return 0;
}

insert image description here
Note: Similar to ordinary loops, you can use continue to end this loop, or use break to jump out of the entire loop.

2. Use references in scope for (some weird problems)

After reading the above code, if we want to use the range for to change the value in the array, what should we do? In fact, it's easy to handle. We only need to change the variables in the scope for to reference types.
For example:

#include<iostream>
using namespace std;
void TestFor3()
{
    
    
	int array[] = {
    
     1, 2, 3, 4, 5 };
	//定义一个变量 e 然后每次都从array取数据赋值给e,自动判断结束
	for (auto& e : array)
	{
    
    
		e *= 2;
		cout << e << " ";
	}
}
int main()
{
    
    
	TestFor3();
	return 0;
}

insert image description here

When you see this, if you are careful, you will think: "How can the variable e referenced here change the binding?" In fact, the variable e in the scope for is recreated every iteration, so the reference here It is equivalent to being initialized continuously.

Of course, there is another example in the for() loop that is created in the same way as the variable e here, and another example is different from the way the variable e is created here,
①same

#include<iostream>
using namespace std;
int main()
{
    
    
	int i = 0;
	for (i = 0; i < 10; i++)
	{
    
    
		int a = 10;
		cout << a << " ";
		a++;
	}
	return 0;
}

insert image description here

This is because a is a local variable, and a will be recreated every time the loop is re-circulated, similar to the use of references in the scope for above.

② Different
variables created in the initialization list of the for() loop will not be recreated every time the loop is re-looped , but when the for() loop ends, the variables created in the initialization list of the for() loop will also disappear.

#include<iostream>
using namespace std;
int main()
{
    
    
	for (int i = 0; i < 10; i++)
	{
    
    
		cout << i << " ";
	}
	return 0;
}

insert image description here

insert image description here

discuss

The creation of variables in the range for is similar to the first method of the normal for() loop above. They may be implemented in the same way. I don’t know much about this. If you have friends who understand, you can discuss it in the comment area.

3. Conditions for the use of scope for

  • The range of the for loop iteration must be determined.
    For an array, it is the range of the first element and the last element in the array; for a class, methods of begin and end should be provided, and begin and end are the range of the for loop iteration. .
    Note: There is a problem with the following code, because the function does not pass an array, and the array name represents the address of the first element, so the range of for cannot be determined.
void TestFor(int array[])
{
    
    
	for(auto& e : array)
		cout<< e <<endl;
}

Guess you like

Origin blog.csdn.net/qq_65207641/article/details/128921829