tjwrona
tjwrona

Reputation: 9035

Under what circumstances should you prefer to write to cout using std::copy?

I've been reading "Effective STL" by Scott Meyers and I've noticed quite a few examples in the book where he prints the contents of a container by using std::copy to copy the elements to std::cout. To me this just feels very difficult to read compared to the alternative of using a range based for loop (perhaps I just need to get used to it?)

// Given the following
std::vector<int> values = { 1, 2, 3, 4, 5, 6, 7, 8 };

// Print using `std::copy`
std::copy(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, ", "));

// Print using a loop
for (const auto value : values)
{
    std::cout << value << ", ";
}

This post seems to suggest that there is no significant performance gain for using std::copy:

is std::copy faster than std::cout for output?

Do some people really find the std::copy approach more readable than the range based for loop?


Maybe if I only want to print half of the values...

std::copy(values.begin(), values.begin() + values.size()/2, std::ostream_iterator<int>(std::cout, ", "));

...but even then I can just do this:

for (auto it = values.begin(); it != values.begin() + values.size()/2; ++it)
{
    std::cout << *it << ", ";
}

So my question is why would anyone prefer the std::copy approach? Are there any situations where you can prove that std::copy is truly a better option (or the only option)?

Upvotes: 2

Views: 104

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122516

You can look at the possible implementation at cppreference:

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
{
    while (first != last) {
        *d_first++ = *first++;
    }
    return d_first;
}

There is nothing magic about it. It really is just a loop that copies elements from Input to Output. Of course it does not use a range-based for loop (it has iterators not the container). It uses a while instead of for, which is just a cosmetic difference.

Are there any situations where you can prove that std::copy is truly a better option (or the only option)?

No. You can always manually write code that does the same as the algorithm. Consider that standard algorithms are not supposed to be better than handwritten code. They are supposed to be as good as handwritten code and not worse. The real benefit is that using an algorithm leaves less room for mistakes and it can be recognized more easily. There are many different ways to write a loop that copies something, but there is only one std::copy, this actually helps to read code.

So it all boils down to opinions...

Do some people really find the std::copy approach more readable than the range based for loop?

Yes, some people believe that one should minimize the use of handwritten loops in favour of algorithms. However, luckily there can be more than one opinion and if "no raw loops" would be what the standard tries to enforce, then probably range-based for loops would have never been included.

Upvotes: 4

Related Questions