Many languages have the Range-based for loops function. Now C ++ knows how to add this important function to the syntax. I really don't know how to translate this function. I have not learned Chinese.
Basic syntax
[Cpp]
For (range_declaration: range_expression) loop_statement
For example:
[Cpp]
Vector <int> vec;
Vec. push_back (1 );
Vec. push_back (2 );
For (int I: vec)
{
Cout <I;
}
The for language can easily traverse containers such as vector. What kind of things can be traversed? This is what we will talk about later.
For complicated containers, you can use the auto statement to simplify the type declaration.
[Cpp]
Map <string, string> complexVector;
For (auto complexVectorEntry: complexVector)
{
Cout <complexVectorEntry. first <"<" <complexVectorEntry. second <">" <endl;
}
In both examples, the value of the traversal element is not modified. If you want to modify their values, use the reference type
[Cpp]
Vector <int> vec;
Vec. push_back (1 );
Vec. push_back (2 );
For (auto & I: vec)
{
I ++;
}
Note that you can still use the continue statement to start the next iteration and use the break to jump out of the loop. This is the same as a common for loop.
In-depth analysis
[Cpp]
For (range_declaration: range_expression) loop_statement
"Equivalent"
[Cpp]
{
Auto & _ range = range_expression;
Auto _ begin = begin_expr (_ range );
Auto _ end = end_expr (_ range );
For (;__ begin! = _ End; ++ _ begin ){
Range_declaration = * _ begin;
Loop_statement
}
}
Note that "equivalent" does not mean that the compiler implements range-based for loops. It means that the running effect of the two is equivalent.
The definitions of begin_expr and end_expr are as follows:
For the array types begin_expr and end_expr, they are equal to (_ range) and (_ range + _ bound)
For containers in STL, the two are equal to _ range. begin () and _ range. end () respectively ()
For other types, the two are equal to begin (_ range) and end (_ range ). The compiler will find the appropriate begin and end functions through parameter types.
Allow custom classes to iterate
Through the equivalent statement of ranged-based for loops, we can see that as long as it meets certain requirements, you can also put your own defined classes in ranged-based for loops for iteration. In fact, to iterate, a class must meet the following conditions:
The begin and end functions point to the first and last data respectively. The returned value is an iterator that can be defined by yourself. It can be either a member function or a non-member function.
The iterator itself supports * ++! = Operator, which can be either a member function or a non-member function.
For example, the following class:
[Cpp]
# Include <iostream>
Using namespace std;
Class IntVector;
Class Iter
{
Public:
Iter (const IntVector * p_vec, int pos)
: _ Pos (pos)
, _ P_vec (p_vec)
{}
// These three methods form the basis of an iterator for use
// A range-based for loop
Bool
Operator! = (Const Iter & other) const
{
Return _ pos! = Other. _ pos;
}
// This method must be defined after the definition of IntVector
// Since it needs to use it
Int operator * () const;
Const Iter & operator ++ ()
{
++ _ Pos;
Return * this;
}
Private:
Int _ pos;
Const IntVector * _ p_vec;
};
Class IntVector
{
Public:
IntVector ()
{
}
Int get (int col) const
{
Return _ data [col];
}
Iter begin () const
{
Return Iter (this, 0 );
}
Iter end () const
{
Return Iter (this, 100 );
}
Void set (int index, int val)
{
_ Data [index] = val;
}
Private:
Int _ data [100];
};
Int
Iter: operator * () const
{
Return _ p_vec-> get (_ pos );
}
Int main ()
{Www.2cto.com
IntVector v;
For (int I = 0; I <100; I ++)
{
V. set (I, I );
}
For (int I: v) {cout <I <endl ;}
}