it makes an int array v with all values from 0 to 9 (int v[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };)
then for all elements in that array (for (auto x : v)) that element and a new-line is printed out (std::cout << x << '\n')
after that it loops through an array that doesn't have a name and loops through that values and prints them + a newline
after that the application freezes in a while(1) loop
So when defining, ( auto x : v ) you are effectively saying, for every element of v assign x to it and print it out, so in every iteration of std::cout in this program, ovewrites the x variable with the next itteration of element, in the array and prints it? correct?
The same for the next part of the function but instead of defining the array as v, the array is defined expliciltly?
And what is the " : " operator called and are there any other uses for it other than defining something to exist in something else like this? (If i got that right)
for(auto x : v) is actually only available in c++11 and newer standards.
It's a shorter version to loop an STL container
for every element in v assign x to it and do something with it
yeah, that's correct
it is basically equivalent to something like:
1 2 3 4 5
for(int i = 0; i < v.size(); ++i) {
auto x = v[i]; // copy constructor
// stuff
}
Note that auto is whatever type is stored in v
for every element of v assign x to it and print it out, so in every iteration of std::cout in this program, ovewrites the x variable with the next itteration of element, in the array and prints it? correct?
Not completely correct.
x is not overwriten, it goes out of scope after each element-iteration and is copy-constructed at the begin of 1 element-iteration
And what is the " : " operator called and are there any other uses for it other than defining something to exist in something else like this? (If i got that right)
I don't know what it's called but the for loop is the only place where it is used.
I almost always (unless I need to modify array) use auto&& anyway, and it turns out it does do sth. And btw it's interesting my vs 2015 doesn't print out dtors after loop-end message.
And btw it's interesting my vs 2015 doesn't print out dtors after loop-end message.
you don't have the dtors after for-loop end? O.o
I mean, i would believe it if you'd have no messages between start and end because you just have a reference but the dtors should exist because they are in the vector o.O
I almost always (unless I need to modify array) use auto&&
you should use const auto& if you don't modify it because you can modify auto&&
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Example program
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<int> vi = {1,2,3,4,5};
for(auto&& a : vi)
a = 5;
for(auto&& a : vi)
std::cout << a <<std::endl;
}
okey good, still note that you can modify a value of type auto&&
@andreywestken, thank you
I wanted to back off while writing this because it was a technically wrong example but I thought it would still serve the purpose
Oh, yeah I wanted to write about that and I forgot. It's been a bad day for me. I thought you cannot modify auto&& since it's r-value reference, but now it makes perfect sense why you can make const auto&&, definetly not only for SFINAE.
there is no advantage over just writing const auto& for iterating a container...
usually you only write auto&& if you want to move from that object because it's an rvalue and therefore not needed anymore so please don't use it for iterating a container.
ok, thanks
P.S. Did you know Gamer2015 ,that when you post piece of code with main() function in code tags you can click on that little gear next to top right corner to run program in cpp.sh? You don't have to bother posting links manually. I appreciate that, but it's not needed :)