What is the difference between for loops and for each loops regarding with using arrays? Can you give example of how for each loop can be used ? What are the advantages ?
@BlueSquirrelJQX
Using the range based for loop is fine with arrays.
Generally range based for loops can be used with all containers where begin()/end() can be used (either as member or as free functions) which includes arrays.
what is the advantage of it comparing to a regular for loop?
@BlueSquirrelJQX , it is mentioned in my book that range-for loops exist like the example that I provided.
@coder777, I heard someone mentioned that this kind of for loop offers a better solution when we do not know how many values the array contains, so we don't know the size of the array which can be a bit frustrating. However, using for each loop can print the values out even if the size is unknown.
I know that you already mentioned "There is no more secret behind" , but does my explanation provide any further clue to what else they might do ?
The size of the array must be known in order to use it in a range based loop. In other words: You can't do more with a range based loop than a normal loop. Actually a range based loop is more limited.
#include <iostream>
#include <iterator>
class Integer {
int a; //some kind of big number class would be used instead of int
public:
Integer(int a) : a(a) {}
booloperator==(Integer rhs) { return a == rhs.a; }
booloperator!=(Integer rhs) { return a != rhs.a; }
Integer& operator++() { ++a; return *this; }
friend std::ostream& operator<<(std::ostream& out, Integer rhs) { out << rhs.a; };
intoperator*() { return a; }
staticconstint Infinity = -1; //would use some kind of enum probably? and add more comparison operators
};
class Counter {
Integer current;
public:
Counter(Integer start) : current(start) {}
Counter& operator++() { ++current; return *this; }
intoperator*() { return *current; }
booloperator!=(Counter rhs) { returntrue; }
};
class Naturals {
public:
Counter begin() { return Counter(0); }
Counter end() { return Counter(Integer::Infinity); }
};
int main() {
for(Integer x : Naturals{}) {
std::cout << x;
if(x == 5) break;
else std::cout << ", ";
}
}
Functional programming languages typically do stuff like this and rely on lazy evaluation (such as a break; in the middle of a loop of infinite values) I could probaby use ForIterator and accomplish putting an infinite list into a range-based for loop, but I have to go to work now. http://stackoverflow.com/a/14920606/2607949
Edit: I decided to give another go at this infinite range-based for code after work, so above is my edited example.
Edit 2: I liked this other syntax a bit better even though it made the code longer.
Says very clearly that here we do something with each element of range bar.
We can write this loop without digging up how many elements are in that range. The compiler will do it for us. The less we have to write, the less we do make mistakes.
That is not as versatile as the old syntax (although you can do black magic and obscure tricks to defeat the clarity with almost every bit of the language).
As we all know, the First Amendment to the C++ Standard states: "The committee shall make no rule that prevents C++ programmers from shooting themselves in the foot."