[LC++]The behavior of istream_iterator<string> as a pointer.
Roberto Diaz
roberto at vivaldi.dhis.org
Fri Dec 28 16:45:04 UTC 2001
On Thu, Dec 27, 2001 at 01:48:49PM +0200, Shaul Karl wrote:
> Aren't the increment operator and comparison work with
> istream_iterator<string>?
Yes.. it works..
> 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, "}");
^^^^^^
Since you are repositioning your stream iter1 is not valid anymore..
> for (; iter1 != iter2; iter1++) cout << *iter1;
> cout << "\n";
> }
>
see this code, it works:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int
main()
{
ifstream input("input_iterator.cc");
istream_iterator<string> in(input), it1, eof;
it1 = find (in, eof, "main()");
// now you have your stream pointer positioned!! if you make a second
// find like this:
// it2 = find (int, eof, "}");
// then it1 will not be valid anymore.. and you only could to use ++it2 not
// ++it1
for (; *it1 != "}"; it1++) cout << *it1 << endl;
return 0;
}
It would be usefull to have a rewind()/seek() method to use in the ifstream..
Yes these iterators are not working as expected...
Stroustrup's book is so badly written!!! aggggggggggg!! it is not that C++ is
bad!!!
--
Saludos
Roberto Díaz <roberto at vivaldi.dhis.org>
PLEASE HELP SAVE faqs.org!
http://www.faqs.org/save_faqs-org.html
More information about the tuxCPProgramming
mailing list