Range for loop:
1. Range-based for loop
for (element type element object: Container object)
{
Loop body
}
(1.1) If the loop body consists of a single statement or individual structure block, you can omit the curly braces
(1.2) The element object is combined with each element of the container object in turn, and each element is combined to perform the loop body sequentially until all the elements within the container are combined.
(1.3) Not dependent on subscript element, Universal
(1.4) No need to access iterators, transparent
(1.5) No need to define the processing function, concise
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
Using namespace std;
Void print(int i)
{
Cout << i << " ";
}
Int _tmain(int argc, _TCHAR* argv[])
{
Int ai[] {65, 66, 67, 68, 69};
Size_t size = sizeof(ai) / sizeof(ai[0]);
Vector<int> vi(ai, ai + size);
/ / Based on the for loop of the subscript operation, not all containers support subscript operations, not universal
For (size_t i = 0; i < size; ++i)
Cout << ai[i]<<" ";
Cout << endl;
For (size_t i = 0; i < vi.size(); ++i)
Cout << vi[i]<<" ";
Cout << endl;
/ / Based on the iterator-based for loop, you need to specify the two ends of the container, and do the self-increment of the iterator, you must understand the operation rules of the iterator, not transparent enough
For (int *it = ai; it != ai + size; ++it)
Cout << *it << " ";
Cout << endl;
For (auto it = vi.begin(); it != vi.end(); ++it)
Cout << *it << " ";
Cout << endl;
/ / Based on the for loop of the generic function, you need to provide a processing function for the element, which is cumbersome for general traversal
For_each(ai, ai + size, print);
Cout << endl;
For_each(vi.begin(),vi.end(), print);
Cout << endl;
For (auto a : ai)
Cout << a << " ";
Cout << endl;
For (auto a : vi)
Cout << a << " ";
Cout << endl;
For (auto &a : ai)
++a;
For (auto &a : vi)
--a;
For (auto const &a : ai)
Cout << a << " ";
Cout << endl;
For (auto const &a : vi)
Cout << a << " ";
Cout << endl;
For (char a : ai)
Cout << a << " ";
Cout << endl;
For (char a : vi)
Cout << a << " ";
Cout << endl;
Getchar();
Return 0;
}
2. Considerations for Scope Loops
(2.1) Use range loops for map and Multimap containers, each time you get an element that is neither a key nor a value, but a pair that consists of a key and a value
(2.2) When using a range-based for loop, you cannot violate the constraints of the container itself
(2.3) range-based for loop, no matter how many times the loop body executes, the expression after the colon is never executed once
(2.4) A range-based for loop, whose underlying implementation still relies on the container's iterator,
Therefore, any structural changes that could cause an iterator to fail can lead to undefined consequences
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
Using namespace std;
Void print(int i)
{
Cout << i << " ";
}
Int _tmain(int argc, _TCHAR* argv[])
{
Int ai[] {65, 66, 67, 68, 69};
Size_t size = sizeof(ai) / sizeof(ai[0]);
Vector<int> vi(ai, ai + size);
/ / Based on the for loop of the subscript operation, not all containers support subscript operations, not universal
For (size_t i = 0; i < size; ++i)
Cout << ai[i]<<" ";
Cout << endl;
For (size_t i = 0; i < vi.size(); ++i)
Cout << vi[i]<<" ";
Cout << endl;
/ / Based on the iterator-based for loop, you need to specify the two ends of the container, and do the self-increment of the iterator, you must understand the operation rules of the iterator, not transparent enough
For (int *it = ai; it != ai + size; ++it)
Cout << *it << " ";
Cout << endl;
For (auto it = vi.begin(); it != vi.end(); ++it)
Cout << *it << " ";
Cout << endl;
/ / Based on the for loop of the generic function, you need to provide a processing function for the element, which is cumbersome for general traversal
For_each(ai, ai + size, print);
Cout << endl;
For_each(vi.begin(),vi.end(), print);
Cout << endl;
For (auto a : ai)
Cout << a << " ";
Cout << endl;
For (auto a : vi)
Cout << a << " ";
Cout << endl;
For (auto &a : ai)
++a;
For (auto &a : vi)
--a;
For (auto const &a : ai)
Cout << a << " ";
Cout << endl;
For (auto const &a : vi)
Cout << a << " ";
Cout << endl;
For (char a : ai)
Cout << a << " ";
Cout << endl;
For (char a : vi)
Cout << a << " ";
Cout << endl;
Getchar();
Return 0;
}
3. Allow the container type you define to support range loops
A class can support a range-based for loop as long as it provides a begin and end function that gets the start and end iterators separately
1 #include "stdafx.h"
2 #include <iostream>
3 using namespace std;
4
5 template <typename T, size_t S>
6 class Array
7 {
8 public:
9 T &operator[](size_t i)
10 {
11 return m_array[i];
12 }
13 T const &operator[](size_t i)const
14 {
15 return const_cast<Array&>(*this)[i];
16 }
17 //Get the starting iterator
18 T *begin()
19 {
20 return m_array;
twenty one }
22 T const *begin()const
twenty three {
24 return const_cast<Array *>(this)->begin();
25 }
26 //Get the terminator
27 T *end()
28 {
29 return m_array+S;
30 }
31 T const *end()const
32 {
33 return const_cast<Array *>(this)->end();
34 }
35 private:
36 T m_array[S];
37 };
38
39
40 int _tmain(int argc, _TCHAR* argv[])
41 {
42 int i = 0;
43 Array<int, 5> ai;
44 for (auto &a : ai)
45 a = ++i * 10;
46 auto const &cai = ai;
47 for (auto &a : cai)
48 cout << /*++*/a << " ";
49 cout << endl;
50 getchar();
51 return 0;
52 }
C++11 range for loop