[LC++]The behavior of istream_iterator<string> as a pointer.
Shaul Karl
shaulka at bezeqint.net
Thu Dec 27 22:50:04 UTC 2001
Aren't the increment operator and comparison work with
istream_iterator<string>?
The following program tries to copy part of its own source file
(main.cc) to cout.
$ cat main.cc
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main()
{
ifstream input("main.cc");
istream_iterator<string> iter1(input), iter2, eof;
iter1 = find(iter1, eof, "main()");
iter2 = find(iter1, eof, "}");
for (; iter1 != iter2; iter1++) cout << *iter1;
cout << "\n";
}
$
When executing it, all I get is the newline from the last statement:
$ ./main
$
Further more, adding the lines
cout << "*iter1 = " << *iter1 << " *iter2 = " << *iter2 << '\n';
iter1 == iter2 ? cout << "iter1 == iter2\n"
: cout << "iter1 != iter2\n";
just before the for loop reveals that
*iter1 = main() *iter2 = }
iter1 == iter2
How could this be? Aren't input iterators supposed to be somewhat like
input pointers?
The compiler is g++-3.0.
--
Shaul Karl
email: shaulka(at-no-spam)bezeqint.net
Please replace (at-no-spam) with an at - @ - character.
(at-no-spam) is meant for unsolicitate mail senders only.
More information about the tuxCPProgramming
mailing list