M
mlt
Will the new java For each construct be integrated into C++?
Will the new java For each construct be integrated into C++?
Will the new java For each construct be integrated into C++?
Ok so C++ already supports something like this:
Jeff Schwab said:[Top-posting elided.]
What is the java For each construct? And why would a java construct
be integrated into C++?
Currently, there's the std::for_each() algorithm, and in C++0x, there
will be a new for loop syntax to iterate over an entire container.Ok so C++ already supports something like this:
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
C++ supports several constructs that are varying degrees of "like this."
The closest thing compatible with the current standard is probably either
Boost.Foreach, or std::for_each with a Boost.Lambda expression. The most
commonly used is probably either the plain old for-loop or std::for_each
with a separately defined functor. The closest available in the core
language probably is the new syntax mentioned by red floyd, but it's still
not standard, and not available in most compilers.
mlt said:Ok this compiles and gives the correct results:
std::vector<int> U;
U.push_back(1);
U.push_back(2);
U.push_back(3);
for each(int u in U) {
std::cout << "u = " << u << std::endl;
}
so "For each" actually is working in C++ like in java.
Victor Bazarov said:Those are probably extensions in Visual C++. Or managed code.
V
Ok this compiles and gives the correct results:
std::vector<int> U;
U.push_back(1);
U.push_back(2);
U.push_back(3);
for each(int u in U) {
std::cout << "u = " << u << std::endl;
}
so "For each" actually is working in C++ like in java.
Ok so C++ already supports something like this:
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
Assuming that you refer to this:
for (TimerTask t : c)
t.cancel();
No, C++ does not already support something like this.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.