-
July 18th, 2009, 02:53 AM
#1
[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.
-
July 18th, 2009, 03:15 AM
#2
Re: Reverse Copy Myth
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.
-
July 18th, 2009, 04:11 AM
#3
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.
-
July 18th, 2009, 11:53 AM
#4
Re: Reverse Copy Myth
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|