CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    [RESOLVED] Reverse Copy Myth

    Hello to all, i did a small experiment where i try out the reverse_copy like

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    #include <deque>
    #include <list>
    #include <algorithm>
    #include <functional>
    #include <utility>
    
    using namespace std;
    
    int main()
    {
     deque<string> cont, temp(7);
    
     cont.push_back("A");
     cont.push_back("B");
     cont.push_back("C");
     cont.push_back("D");
     cont.push_back("E");
     cont.push_back("F");
    
    // 1
    reverse_copy(cont.begin(), cont.end(), temp.begin());
    copy(temp.begin(), temp.end(), ostream_iterator<string>(cout, "\n"));
    
    // 2
    reverse_copy(cont.begin(), cont.end(), cont.begin()); 
    copy(cont.begin(), cont.end(), ostream_iterator<string>(cout, "\n"));
    
    return 0;
    Why the first produce expected output and second not did the right thing ?

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Reverse Copy Myth

    Quote Originally Posted by Peter_APIIT
    Why the first produce expected output and second not did the right thing ?
    The second call of reverse_copy violates the pre-condition that the destination range shall not overlap with the source range.

    By the way, it might be more intuitive to create temp with as many elements as cont, then explicitly print a newline between the two calls of reverse_copy. I was a little puzzled by the blank line divider when I tried your program, until I realised that that was the effect of an empty string in temp. Oh, and you included so many headers that are unnecessary, but did not #include <string> and <iterator> for std::string and std::ostream_iterator respectively.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reverse Copy Myth

    Code:
    template <class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy ( BidirectionalIterator first,
                                    BidirectionalIterator last, OutputIterator result );
    Thanks laserlight. I thought there are all the same since bidirectional iterator also a ouput and Input Interator.

    What blank divider you talking about ?
    Thanks for your help.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Reverse Copy Myth

    Quote Originally Posted by Peter_APIIT
    What blank divider you talking about ?
    The fact that there is a blank line between the first portion of output and the second portion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured