//Wrong Code
#include<iostream>
#include<iterator>
using namespace std;
int main(){
istream_iterator<string> in_iter(cin),eof;
ostream_iterator<string> out_iter(cout," ");
while(in_iter!=eof)
*out_iter++ = *in_iter++;
}
Input:(Run the code above in interactive mode through "visual c++"、"cpp.sh"、"onlinegdb" and whatever tool you like...)
aa bb cc 6 dd ee
Output:
aa bb cc 6 dd
Actually the correct code should be:
#include<iostream>
#include<iterator>
using namespace std;
int main(){
istream_iterator<string> in_iter(cin),eof;
ostream_iterator<string> out_iter(cout," ");
while(in_iter!=eof){
*out_iter = *in_iter;
out_iter++;
in_iter++;
}
}
Input:
aa bb cc 6 dd ee
Output:
aa bb cc 6 dd ee
Description: Very easy code. Just for printing some characters and output will be the same. However, in the book of "C++ Primer", as the picture shown below, it gives us a wrong code. Is it a bug of C++ or just the mistake of "C++ Primer"? Picture of "C++ Primer"