Consider I have the following “pair of floats” object input format: (first variable, second variable) e.g. (1.0, 15.6).
What’s the best way reading such structures? In C I would use scanf(“(%f, %f)”, &var1, &var2) – pretty nice, isn’t it?(yes, I know it doesn’t provide type safety and so on)
But I know only one way to do that using C++ streams:
float var1, var2;
char tmp;
cin >> tmp;
cin >> var1;
cin >> tmp;
cin >> var2;
cin >> tmp;
Seems ugly, and it’s just a pair of floats. So, is there an elegant way to do that? Like
cin >> "(" >> var1 >> ", " >> var2 >> ")";
I would write an input operator:
Stealing FloatPair from @Seth Carnegie.
Thus input now looks normal:
I would make it look like this.
Then I have an ignore object like this.
It is simple to templatise if you wish. And to make the code slightly easier I call mine
IThen the input operator>> for the ignore looks like this.
A specialization for string. To handle the fact that operator>> on string only reads a word.
Note: in scanf() a space matches 1 or more spaces. Thus obeys the same rule if the input string has a space in it.