f



std::copy_backwards vs std::reverse_copy vs std::copy with reverse iterators

It seems all three can be used to accomplish almost identical tasks.
Can someone chime in with their thoughts on their relative merits?

As a concrete example, recently I was using reverse iterators to
push_back on a vector in reverse order.

std::vector<Elem> srcVec;
std::vector<Elem> dstVec;
std::copy(srcVec.rbegin(),
              srcVec.rend(),
              std::back_inserter(dstVec));

It occured to me that std::copy_backwards, and std::reverse_copy could
be used to accomplish the same task, but I wasn't clear on why both
alternatives are available.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Brendan
9/15/2010 10:27:37 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
1391 Views

Similar Articles

[PageSpeed] 51

On 15 Sep., 18:27, Brendan <catph...@catphive.net> wrote:
> It seems all three can be used to accomplish almost identical tasks.
> Can someone chime in with their thoughts on their relative merits?

I would always attempt to use the most reasonable
algorithm available for my data structures.

First, both std::reverse_copy and std::copy_backward
impose very different requirements on the iterators,
and the processes are different as well: The more
fundamental one is reverse_copy, because the destination
is just an output iterator, such that the reordering
is done taking advantage of the source iterator properties.
std::copy_backward requires that both destination and source
are bidirectional iterators, but allows for more
overlap between source and destination.

Second, yes, some algorithms can be realized by
using a corresponding iterator adapter, but the
algorithms are still useful by themselves and for
very fundamental algorithms this basic functionality
is provided even though it can be simulated by
applying the correct iterator adaptor to another
algorithm. Among other examples, std::move
was provided, even though there exists
std::move_iterator.

HTH & Greetings from Bremen,

Daniel Kr=FCgler

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
ISO
9/16/2010 10:23:47 AM
On Sep 15, 8:27=A0am, Brendan <catph...@catphive.net> wrote:
> It seems all three can be used to accomplish almost identical tasks.
> Can someone chime in with their thoughts on their relative merits?
>
> As a concrete example, recently I was using reverse iterators to
> push_back on a vector in reverse order.
>
> std::vector<Elem> srcVec;
> std::vector<Elem> dstVec;
> std::copy(srcVec.rbegin(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 srcVec.rend(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 std::back_inserter(dstVec));
>
> It occured to me that std::copy_backwards, and std::reverse_copy could
> be used to accomplish the same task, but I wasn't clear on why both
> alternatives are available.

IMO, this is all redundant the creates more opportunities for code
obfuscation.

std::vector<Elem> srcVec;
std::vector<Elem> dstVec;
std::copy_backwards(srcVec.rbegin(),
                     srcVec.rend(),
                     std::back_inserter(dstVec));

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Hakusa
9/16/2010 3:50:36 PM
On Sep 16, 9:23�am, Daniel Kr�gler <daniel.krueg...@googlemail.com>
wrote:
> On 15 Sep., 18:27, Brendan <catph...119@catphive.net> wrote:
>
> > It seems all three can be used to accomplish almost identical tasks.
> > Can someone chime in with their thoughts on their relative merits?
>
> First, both std::reverse_copy and std::copy_backward
> impose very different requirements on the iterators,
> and the processes are different as well: The more
> fundamental one is reverse_copy, because the destination
> is just an output iterator, such that the reordering
> is done taking advantage of the source iterator properties.
> std::copy_backward requires that both destination and source
> are bidirectional iterators, but allows for more
> overlap between source and destination.

Ok, I think I see where some of my confusion was now on reading your
post, and going over Josuttis a little more closely.

copy_backward doesn't reverse anything, it merely copies the starting
at the end of the source range, to the end of the dest range. I didn't
notice that the third argument was marked destEnd originally.

The difference in iterator requirements is also a good point.

So in my example, which used a back inserter, reverse_copy would have
been the best choice, although copy with reverse iterators works.
copy_backwards should fail to compile because back inserters are
output iterators, not bidirectional iterators.

Thanks for your response.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Brendan
9/17/2010 2:51:00 AM
Reply:

Similar Artilces:

Iterating a std::vector vs iterating a std::map?
I have a std::vector and a std::map containing the same number of elements. Currently I experience that iterating all elements in the map is slower that iterating all elements in the vector. Is this true ? carl wrote: > I have a std::vector and a std::map containing the same number of > elements. Currently I experience that iterating all elements in the map > is slower that iterating all elements in the vector. Is this true ? Do you want us to decide if you are hallucinating or not? You have said: you are experiencing it. So I guess then it is true. :) If your ...

std::container::iterator vs std::container::pointer
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs using ::pointer? I did a quick experiment and replaced all the ::iterator in my code with ::pointer and it seems to work the same. What I'm think is on the surface they're the same but perhaps there're some subtle differences beneath the surface that I'm unaware of? Thanks "Vivi Orunitia" <Vivi@blackmagevillage.com> wrote in message news:Xns94841E297AB88...

std::istream slowness vs. std::fgetc
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a function to count lines in a large file that start with a particular pattern. The file can contain large amounts of non-text crap, which may make some lines very long (so using getline() with a std::string isn't feasable). So I dug around looking at what else is available in terms of the unformatted io functions and such, and found istream::ignore, but the performance was crap compared to ...

std::string::push_back vs. std::string::operator+=
Hi, I just browsed libstdc++6-doc and stumbled over a string operation I haven't noticed before: string::push_back. If for example I want to append single characters to an std::string, which one would be better, or is there any difference at all: mystr.push_back( c ); or mystr += c; Any ideas? I used to use the latter one. Regards, Matthias Matthias K�ppler wrote in news:cntj5d$ooa$03$1@news.t-online.com in comp.lang.c++: > Hi, > > I just browsed libstdc++6-doc and stumbled over a string operation I > haven't noticed before: string::push_back. push_back() is ther...

std::stack vs std::deque (stacked deck?)
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways coming back, I've fallen in love with all the improvements in the language such as the STL. Congrats to anyone who worked on it, you did an excellent job. To teach myself c++ again, I decided a good first project would be to implement a scripting language against a MUD server/client system I originally designed in C. Thus far I've made what I feel is incredible headway, and thanks to this newsgro...

std::terminate per thread
Hi! Since C++ never talked about threads, MS was quite free to specify[1]: set_terminate (CRT) ------------------- (Visual Studio 2010 ) Installs your own termination routine to be called by terminate. ... In a multithreaded environment, terminate functions are maintained separately for each thread. Each new thread needs to install its own terminate function. Thus, each thread is in charge of its own termination handling. Now, looking at the latest std draft (N3242): 18.8.3.1 Type terminate_handler 2 Required behavior: A termi...

std::atomic_bool vs. std::atomic<bool>
These types seem to have almost identical interfaces. The only difference I see (in terms of functions that may be called -- I didn't check the semantics of the functions) is that std::atomic<bool> offers bool operator=(bool); and std::atomic_bool does not. (It almost offers that function, but it's volatile-qualified.) Can somebody please explain why we need std::atomic_bool instead of just using std::atomic<bool>? Thanks, Scott -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mai...

Implementing operator<, std::mismatch vs. std::lexicographical_compare
I implemented operator== for my container type with std::equal. I was about to use std::lexicographical_compare for my operator<, but I realized it may be inefficient: 1. Return special-case value if one or both lists end early. 2. You do *l < *r and return TRUE if this comparison does so. 3. Else, do *r++ < *l++ and return FALSE if this comparison returns true. 4. Otherwise, you're currently equal and go back to (1). The lexicographical compare makes its decision at the first unequal pair of elements (or FALSE if all are equal). Instead of two calls to operator...

"using namespace std" Vs "std::..."
I am a C++ beginner. I have read some articles about "using namespace std" and "std::". What is the exactly different between them? What bad if I use "using namespace std" in my program instead of using "std::cout","std::cin".. and so on. Thanks. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] On 20 Oct 2003 17:50:05 -0400, scotthct@hotmail.com (Scott) wrote: >I am a C++ beginner. I have read some articles about "using namespace >std" and &q...

std::any and std::exception_ptr
N3804 proposes the introduction of std::any. Now, C++ already has an "any"-like type, namely std::exception_ptr. Semantically, after the introduction of std::any, exception_ptr will be a special case of std::any, namely a semantical std::any initialized by a caught exception. Wouldn't it make sense to make these two interoperate nicely? In particular, ** std::exception_ptr should be able to be converted into a std::any, as, currently, the only way of looking into an exception_ptr is re-throwing and catching immediately (please correct me if I am wrong). ** std::rethro...

std::any and std::exception_ptr
{ edited by mod to shorten lines to ~70 characters. -mod } [mistakenly posted to comp.lang.c++ before] N3804 proposes the introduction of std::any. Now, C++ already has an "any"-like type, namely std::exception_ptr. Semantically, after the introduction of std::any, exception_ptr will be a special case of std::any, namely a semantical std::any initialized by a caught exception. Wouldn't it make sense to make these two interoperate nicely? In particular, ** std::exception_ptr should be able to be converted into a std::any, as, currently, the only way of look...

How to use std::copy with std::map?
Should the following work? It does on some compilers. How can I get it to work on g++ 3.2? On g++ 3.2 it keeps telling me that there is no such function operator << for ostream and pair<string,string>. But there is! And I can call it! And it works! So why cannot std::copy find it? thanks, Siegfried template<typename K, typename V> ostream& operator<<(ostream& os, std::pair<K,V>& p){ return os << p.first << p.second; } std::map<string,string> m; m[(string)"hello"] = (string)"there"; std::copy(m.begin(), m.end(...

std::operator<<(basic_ostream&, type) vs. std::basic_ostream::operator(type) ... ?
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> >(std::basic_ostream<char,std::char_traits<char> > & _Ostr={...}, ___) will be called. However, I am using the boost::format library that internally makes use of a string stream to store the formatted string. There: std::basic_ostream<char,std::char_traits<char> >::operator<<(___) will always be called. On what basis will the compiler choose which...

SFINAE vs error reporting vs std::get
Hi! I was just working on a generic `get_index_sequence<T>` metafunction that would automatically compute an index sequence for any type depending on the availability of `get<0>`, `get<1>`... for `T`. My goal is to be able to make a more generic version of the proposed `std::apply` that would work for pairs, tuples, arrays and any user type that defines `get` for 0..N However, this approach is not working on Clang 3.6 because Clang is "smart" and uses a `static_assert` when `N` is out of range in `std::get<N>`. I have experienced similar pro...

Web resources about - std::copy_backwards vs std::reverse_copy vs std::copy with reverse iterators - comp.lang.c++.moderated

Resources last updated: 1/28/2016 3:10:32 PM