I have a set of utility functions which read in lines from a stream rather than reading word by word. They work with strings, so I'd like to locally change the string extraction operator within the scope of these functions only. Is that possible?
What I'm doing right now is to create a struct that is a string and writing an extraction operator for that struct only.
struct line{
string str;
};
istream& operator>>(istream& lhs, line& rhs){
return getline(lhs, rhs.str);
}
And then extracting an istream like this:
vector<line> foo{ istream_iterator<line>(istringstream("Lorem Ipsum\nLorem Ipsum")), istream_iterator<line>() };
This works fine but I do not like the struct line wrapper. What I'm asking is, can I locally overload istream& operator>>(istream& lhs, string& rhs) to accomplish this instead?
Aucun commentaire:
Enregistrer un commentaire