Example C ++ output operator overload for data analysis

I am looking for a good example of how to overload a stream input operator (operator →) to parse some data using simple text formatting. I read this tutorial , but I would like to do something more advanced. In my case, I have fixed strings that I would like to check (and ignore). Assume the format of the 2D point from the link is more like

Point{0.3 =>
      0.4 }

where the intended effect is to parse the numbers 0.3 and 0.4. (Yes, this is a terribly stupid syntax, but it includes a few ideas that I need). Basically, I just want to see how to correctly check for fixed lines, ignore spaces, etc.

Update: Unfortunately, the comment I made below has no formatting (this is my first time using this site). I found that you can skip the space with something like

std::cin >> std::ws;

And for eating the strings I have

static bool match_string(std::istream &is, const char *str){
    size_t nstr = strlen(str);
    while(nstr){
        if(is.peek() == *str){
            is.ignore(1);
            ++str;
            --nstr;
        }else{
            is.setstate(is.rdstate() | std::ios_base::failbit);
            return false;
        }
    }
    return true;
}

Now it would be nice to get the position (line number) of the parsing error.

Update 2: Got line numbers and parsing comments using only 1 character ahead. The final result can be seen here in AArray.cpp in the parse () function. A project is a serializable (de) serializable array class based on C ++ PHP.

+5
source share
1 answer

→ (istream &, object &) , / .

( ), istream:: sentry . , setstate( ios_base::failbit ), , my_stream.clear().

. <istream> ( istream.tcc, SGI STL).

+1

All Articles