I have the following code:
#include <iostream>
#include <algorithm>
struct Point
{
int x, y;
Point(int x, int y): x(x), y(y) {}
};
int main()
{
Point arr[] = {
Point(4,2), Point(0,3), Point(1,2)
};
std::sort(arr, arr+sizeof(arr)/sizeof(Point), [](Point a, Point b){return a.x<b.x;});
return 0;
}
Now, i am supposed to write iterative for loop (built in cpp for_each loop) which prints out all of the elements of the array, where, as a iteration variable we must use auto reference.
Now, this confuses me a bit, since i know this can be done without any iteration variables or something, like this:
std::for_each(arr,arr + sizeof(arr)/sizeof(Point), [](Point a){cout<<a.x<<a.y<<std::endl;}
Obviously, this is not what i am asked to do, so, since i never found myself using iteration variables when dealing with for_each loop, i'd like to find out how am i supposed to do that properly, especially considering the fact that i have to use auto reference. Any help appreciated!
for_eachis not a loop, it is a function. Wikipedia has an article on the "foreach" loop construct with examples in numerous languages, including C++.