I would write an input statement:
Theft of FloatPair by @Seth Carnegie.
So the input now looks fine:
FloatPair c; std::cin >> c;
I would do it like this.
std::istream& operator>>(std::istream& stream, FloatPair& out) { return stream >> I('(') >> out.a >> I(',') >> out.b >> I(')'); }
Then I have an ignore object like this.
If you want, just cry. And to make the code a little easier, I call my I
template<typename T> struct ignore { T ignoreItem; ignore(T const& x): ignoreItem(x) {} }; template<typename T> ignore<T> I(T const& x) { return ignore<T>(x);}
Then the input operator โ for ignoring is as follows.
std::istream& operator>>(std::istream& stream, ignore<T> const& test) { T next; if ((stream >> next) && (test.ignoreItem != next))
Specialization for string. To cope with the fact that the โ operator on the line only reads the word.
Note: in scanf (), a space runs 1 or more spaces. Thus, it obeys the same rule if the input string has a space in it.
template<> struct ignore<std::string> { std::vector<std::string> ignoreItemVector; ignore(std::string const& x) { // Split the input into a list of words to ignore. std::stringstream words(x); std::copy(std::istream_iterator<std::string>(words), std::istream_iterator<std::string>(words), std::back_inserter(ignoreItemVector) ); } }; template<> std::istream& operator>><std::string>(std::istream& stream, ignore<std::string> const& test) { // Specifically ignore each word. foreach(std::string const& loop, test.ignoreItemVector) { std::string next; if ((stream >> next) && (loop != next)) // if the stream already failed { stream.setstate(std::ios::badbit); // then don't change anything } // as it may confuse people } return stream; }
source share