C/C ++ has been used for a long time. For Loop statements such as while, do while, and for, the for clause has a special liking for its simplicity, clarity, and flexibility. To access the array type variables, only the statements written by for are the easiest to read, such: Int Arr [N] = { /* */ };
For ( Int I = 0 ; I < N; ++ I)
Printf ( " Arr [% d] = % d \ n " , I, arr [I]);
However, in this case, when STL is reached, it becomes a bit more appealing: For (Vector < Myclass > : Const_iterator ITER = M_vecdata.begin (); ITER ! = M_vecdata.end (); ++ ITER)
{
If ( ! ITER -> Isbusy ())
ITER -> Dosomething (PARAM );
}
Such a long for will no longer give people a clear feeling. Maybe because of this Program It's a little short, and I don't feel too much about it. When I look back at my own program, there are many such expressions, I feel a little upset. Change? For (Size_t I = 0 ; I < M_vecdata.size (); ++ I)
{
If ( ! M_vecdata [I]. isbusy ())
M_vecdata [I]. dosomething (PARAM );
}
Good. It's easy. But here is the example of vector. If it is a list or another container, it will not work.
Other advanced Languages provide foreach or for in statements, which are clearly written: Foreach(ItemInM_vecdata)
{
If(!Item. isbusy ())
Item. dosomething (PARAM );
}
Can C ++ be so simple? As if there is a for_each in STL, try to rewrite it: Struct Ifnotbusythendosomething
{
Ifnotbusythendosomething ( Const Param & Param)
: Param _ (PARAM)
{}
Void Operator ()( Const Myclass & Item)
{
If ( ! Item. isbusy ())
Item. dosomething (Param _);
}
Private :
Const Param & Param _;
};
For_each (m_vecdata.begin (), m_vecdata.end (),Ifnotbusythendosomething(PARAM ));
Yes, the for statement is simple, but there are moreIfnotbusythendosomething definition, whichCodeBut several times more. If every loop is like this, it would be better for me to write for directly. There may be other methods:
Vector < Myclass > Notbusyclass;
Remove_copy_if (m_vecdata.begin (), m_vecdata.end (), inserter (notbusyclass, notbusyclass. Begin (), mem_fun_ref ( & Myclass: isbusy ));
For_each (notbusyclass. Begin (), notbusyclass. End (), bind2nd (mem_fun_ref ( & Myclass: dosomething), Param ));
Oh, it seems more horrible. In addition, not all situations can be used:
1. notbusyclass cannot be a vector <const myclass &>, because a pointer to the reference cannot be created. This requires that myclass be copyable. But even if it is copyable, sometimes the copy cost is very high.
2. The myclass: dosomething parameter cannot be a reference (we often define the parameter as const Param &), because the reference type cannot be defined.
3. Once an error occurs, the error message will be very faint.
it seems that standard C ++ alone cannot be used. The boost Lambda library seems to be very good, and is used:
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> for_each (m_vecdata.begin (), m_vecdata.end (),
if_then (! BIND ( & myclass: isbusy, _ 1),
BIND ( & myclass: dosomething, _ 1, Param);
Yes, it's better, but it's still pretty bad. Is there any better? Yes. boost_foreach: Boost_foreach (cosnt myclass&Item, m_vecdata)
{
If(!Item. isbusy ())
Item. dosomething (PARAM );
}
Oh yeah!
Well, the question is, why does C ++ not directly provide the foreach function in the language?
I personally think there are several reasons:
1. C/C ++ has no built-in containers except arrays, so the for statement is sufficient.
2. When C ++ evolved to STL, the C ++ Standards Committee had no time to consider anything else.
other advanced languages have built-in foreach because they provide standard container libraries and iterative/enumeration interfaces at the beginning. Therefore, foreach is a logical component.
now, C ++ has begun to consider the complexity of the Code caused by the introduction of templates. This is indeed the gospel of cpper. Therefore, a series of related proposals are submitted. The proposals involved in the above Code include: decltype, lambda expressions and closures for C ++, proposal for new for-loop.
the new for loop is the most suitable for foreach. With this statement, the above program can write as follows:
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> for ( const myclass & item: m_vecdata)
{< br> If ( ! item. isbusy ()
item. dosomething (PARAM);
}
However, considering that the decltype & Auto proposal has been adopted, the new for-loop does not know whether it can be accepted. Because after decltype & Auto is used, the program can write as follows:
For (Auto ITER = M_vecdata.begin (), end = M_vecdata.end (); ITER ! = End; ++ ITER)
{
If ( ! ITER-> isbusy ())
ITER-> dosomething (PARAM );
}
it seems to be complicated, right? However, with decltype & Auto, the foreach function can be simulated in the form of libraries or macros. boost_foreach does this. The specific simulation method is clearly written.
at the same time, if the lambda proposal can be approved again, you will be very happy:
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> for_each (
m_vecdata,
(item) extern (PARAM)
{< br> If (! Item. isbusy ()
item. dosomething (PARAM);
}< br>);
Cool!
Mu Feng Xiaozhu
However, the foreach function is added in VC ++ 2008, but the keyword is not foreach, but for each, which is a bit depressing. it is best to replace it with a macro definition to avoid problems with portability.