[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: C++: istream_iterator<string> has no component named operator!=.



On Monday 24 December 2001 11:37 am, Brian Nelson wrote:
> Shaul Karl <shaulka@bezeqint.net> writes:
> > What am I doing wrong?
> > Summary:
> >
> > Breakpoint 1, main () at main.cc:15
> > 15          for (; iter1 != iter2; iter1++) cout << *iter1;
> > (gdb) p iter1 != iter2
> > Structure has no component named operator!=.
> > (gdb)
> >
> > Full details:
> >
> > $ 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, iter2, "main()");
> >     iter2 = find(iter1, eof, "}");
>
>               ^^^^
> These are your problem.  If you put
>
>     cout << "iter1: " << *iter2 << "\titer2: " << *iter2 << "\n";
> immediately following those find statements, you'll get the output
>
>     iter1: }	iter2: }
>

on my system (gcc-2.95.4), at this point 
iter1 == iter2, but *iter1 != *iter2

> which means the find statements aren't doing what you want.  The for
> loop is working fine, however.
>
> I don't know much about istream_iterators, and I don't have any of my
> C++ books on me right now to look it up.  However, I'd guess the problem
> is in your initialization of iter2 and eof.  

They are initialized by default to represent the end of the input.
Don't know exactly what the OP wants to do, but he might try something like 
this, which will print everything between the first line that starts with { 
and the fist one that starts with } afterwards.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() 
{
    string s;
    ifstream in("main.cc");
    while(getline(in, s, '\n') && s[0] != '{');
    while(getline(in, s, '\n') && s[0] != '}') cout << s << '\n';
}

HTH

Alec



Reply to: