0

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!

2
  • And here you find an example on how to do it with a std::for_each instead of a range-for loop, if that's what you're looking for Commented Sep 5, 2019 at 6:35
  • for_each is not a loop, it is a function. Wikipedia has an article on the "foreach" loop construct with examples in numerous languages, including C++. Commented Sep 5, 2019 at 6:54

2 Answers 2

3

If you're looking for a solution based on std::for_each, you can do the following.

std::for_each(std::begin(arr), std::end(arr),
    [](auto& p){ cout << p.x << p.y << "\n"; });
    // ^^^^^ auto reference

Here, you have an auto& reference for the object that you intend to do something with in each iteration (in the case above, it would make sense to use const auto&).

This is almost identical to the range-based for loop suggested by @cdhowie. The only interesting point to note here is that std::for_each is one of the few (the only?) exception to the rule that callables passed to STL algorithms must not have side effects. In this case, writing to the global std::cout object is a side effect, and std::for_each explicitly allows that.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, however, where do i even use auto reference p here?
The lambda gets an auto& reference as a parameter.
2

You're probably looking for the range-for loop:

for (auto & i : arr) {
    std::cout << i.x << ',' << i.y << '\n';
}

7 Comments

Since OP asked for auto ref, I'd suggest changing to auto& i
@nada Indeed, I had it that way originally but thought OP was asking for auto only. Missed the part about it being a reference.
@cdhowie thanks for the reply, however, i thought that i am supposed to use for_each loop in this case since it says "built-in cpp for-each loop" to be used, anyway, im kinda confused about this
@cdummie The "built-in for-each" loop is the range-for loop. They are the same thing... unless your book/professor actually means std::for_each? But from your question it sounds like they did not mean that. An actual quote of what you're expected to do would be helpful.
@cdummie Then it probably is std::for_each and lubgr's answer would be correct. Note that both styles (std::for_each and range-for) are going to generate either identical or nearly-identical machine code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.