For each in C++?

R

red floyd

Will the new java For each construct be integrated into C++?

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.
 
M

mlt

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.

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.


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.
 
N

Noah Roberts

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.

Is this a question or statement of fact? It seems like a question but
'.' would indicate statement of fact.

Either way though, as has been told to you many times...the answer is
no. If you think that means Java is the better language then by all
means, go use it.
 
M

mlt

Victor Bazarov said:
Those are probably extensions in Visual C++. Or managed code.

V


Yes I use Visual Studio 8.0 for Vista 64. Is there any way to know if legal
code in Visual studio is "native C++" or M$ only code?
 
S

SG

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.

No, it isn't. And I won't. It will be much better. :)

In future C++ you will be able to write

for (int u : U) {
std::cout << "u = " << u << std::endl;
}

after including

#include <for>

and as for how it compares to the java version: Java uses runtime
polymorphism for its iterators (since interfaces are the only
abstraction mechanism). C++ (or more specifically its standard
library) uses compile-time polymorphism for iterators. So, such a for-
each loop would work without an abstract base_iterator class. Another
way of saying the same thing is: In C++ you don't need an extra level
of indirection for iterating over some sequence.

Anyhow, this kind of "range loop" is currently not yet available (at
least not officially in non-experimental compilers -- as far as I
know)

Cheers!
SG
 
E

Eric.Malenfant


Assuming that you refer to this:
for (TimerTask t : c)
t.cancel();

No, C++ does not already support something like this.

However, in the latest working draft (n2798) I have for C++0x (the
*next* version of C++), section 6.5.4 has this example, which seems
pretty close to the Java syntax you refer to:

int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,162
Messages
2,570,896
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top