10

In terms of practical usage what differences are there between

a) copy_backward

b) copy with reverse_iterators for source and destination

In particular is one more generally applicable than the other? Are there any other differences?

Update: If there's really no difference, then any reference in C++ literature to this equivalence is appreciated. The motivation behind this question is to understand if this was by design or one of those slip ups (like missing copy_if)

3
  • I wouldn't be surprised if, in practice, using a reverse_iterator requires the iterator type to be properly tagged whereas copy_backward is implemented as a simple loop. Obviously that's not guaranteed, though. As far as the standard is concerned, all iterators must be tagged. May 14, 2012 at 8:27
  • hmm... along the same lines could reverse_iterator perhaps be slower in case the original iterator is a T*?
    – hawk
    May 14, 2012 at 16:39
  • T* is tagged as a random-access iterator, since there is a guaranteed partial specialization template<class T> struct iterator_traits<T*>;. May 15, 2012 at 8:51

5 Answers 5

5

First of all, use of copy_backward() clearly shows developer's intention to copy interval in reverse order.

copy_backward() works with original bidirectional iterators, while the reverse_iterator is an adaptor of a bidirectional iterator and may be not as effective as original iterator.

Using the reverse_iterator makes sense when you need to apply an algorithm for input iterators like copy() to reverse sequence, but don't have counterpart for bidirectional operators like copy_backward().

Therefore, there are conceptual and practical differences.

2

http://www.cplusplus.com/reference/algorithm/copy/ has a implementation of std::copy

http://www.cplusplus.com/reference/algorithm/copy_backward/ has a implementation of std::copy_backward

You can see the differences by yourself.

Note: If I were you I would use std::copy_backward, because calling std::copy with the std::reverse_iterator<T> class might be a little slower (it needs more memory than a bidirectional iterator)

0

The difference is that copy returns an iterator to the pass-the-end element while copy_backward returns an iterator to the first element.

They are not equivalent in that sense.

  • The signatures of course differ. copy is ok with InputIterators and OutputIterator. Whilst copy_backward expects BidirectionalIterators.
  • The effect on the container (when used correctly) is the same, but the returned iterators are of different types and point to different elements.

Example:

This works because vector can use RandomAccessIterator which does support the properties expected by InputIterator, OutputIterator and BidirectionalIterator.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void printer(int i) {
    cout << i << ", ";
}

int main() {

    int mynumbers[] = {3, 9, 0, 2, 1, 4, 5};
    vector<int> v1(mynumbers, mynumbers + 7);

    vector<int>::iterator it = copy_backward(mynumbers, mynumbers + 7, v1.end());
    for_each(v1.begin(), v1.end(), printer);
    cout << endl << "Returned element: " << *it;
    cout << endl;

    vector<int>::reverse_iterator rit = copy(mynumbers, mynumbers + 7, v1.rbegin());
    for_each(v1.begin(), v1.end(), printer);
    cout << endl << "Before the first element (reverse end)? " << (rit == v1.rend());
    rit--; // go to first element, because it is a reverse iterator
    cout << endl << "Returned element: " << *rit;

    return 0;
}

Result:

3, 9, 0, 2, 1, 4, 5, 
Returned element: 3
5, 4, 1, 2, 0, 9, 3, 
Before the first element (reverse end)? 1
Returned element: 5

If you'd use a container that does not support BidirectionalIterator then you risk trouble (e.g. if you'd try to copy backward a forward_list because it uses a ForwardIterator, which is does not support operations provided by a BidirectionalIterator).
Also in this case copying with reverse iterator on forward_list is also not possible, because it does not support reverse iterators.

Basically, you need to make sure the container's iterators are supported and choose according to which end of the container you'd like to have returned. Otherwise the effect is the same.

-1

There no differences practically. In the same manner you can compare forward copying.

a) copy

b) copy_backward with reverse iterators.

3
  • 1
    That's technically not true since copy is more general in that it can be used with input/output iterators. It doesn't require bidirectional iterators.
    – hawk
    May 14, 2012 at 8:11
  • 1
    So you answered your question =) There is a technical difference.
    – inkooboo
    May 14, 2012 at 8:14
  • 2
    @inkooboo: that's a difference between your (a) and (b): one requires at least a bidirectional iterator, and the other doesn't. The same difference doesn't apply to hawk's (a) and (b), since they both require at least a bidirectional iterator. May 14, 2012 at 8:23
-1

There shouldn't be any difference, however: copy_backward comes handy in case the iterator is result of some function call and also much cleaner than using copy with reverse iterators.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.