[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
The behavior of istream_iterator<string> as a pointer.
- To: linux-il(at-nospam)linux.org.il
- Subject: The behavior of istream_iterator<string> as a pointer.
- From: Shaul Karl <shaulka(at-nospam)bezeqint.net>
- Date: Thu, 27 Dec 2001 16:51:56 +0200
- Delivered-To: linux.org.il-linux-il@linux.org.il
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
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?
Compilation line is
g++ -Wall -g -o main main.cc
g++ is either 2.95 or 3.0, I tried with both.
--
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.
=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il