Possible duplicate:
What is the advantage of using universal references in range-based loops?
To iterate over a vector<int> elements to read them, I think the following C ++ 11 use for a range with auto correct:
std::vector<int> v; .... for (auto n : v) {
If the elements stored in the container are not simple integers, but are somewhat "heavier", for example, "t23> s", I understand that to repeat them in vector<string> correct range is used for + auto :
std::vector<std::string> v; .... for (const auto& s : v) {
Using const & avoids unnecessary deep copies and should be OK, because the loop code just watches the contents of the vector.
As far as I understand, still?
Now I saw code that uses a different form of auto in a loop for a loop: auto&& , for example:
for (auto&& elem : container) ....
What is this good for? What is the advantage of using r-value ( && ) references with a range for loops? When should we use this style?
source share