Hey,
here is a small problem i have a little trouble solving myself and finding hints or tipps for on the interwebs:
I want to use copy() from the STL to get input from cin directly to a vector of strings. However, I dont want cin to put single words into the vector, which is what the std extraction operator>> will do, but i want it to read complete lines. For that I will have to locally overwrite the opertator>> from the std namespace.
The following code:
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
vector<string> strVec;
copy(istream_iterator<string>(cin), istream_iterator<string>(),
back_inserter(strVec));
for(string str : strVec)
{
cout << str << '\n';
}
}
|
given the input:
one two three
seven eight nine |
should produce the exact same lines as output.
Up to now, this is meerly my understanding of the task, if something is completely bonkers in there, point it out and i will rethink the whole thing, but as it stands, this is what i m working on.
so, two problems:
i assume i use a anonymous namespace to overwrite operator>> for an istream and a string. i ve got the main from above in the same source file as the following definition:
1 2 3 4 5 6 7
|
namespace
{
std::istream &operator>>(std::istream &istr, std::string &str)
{
getline(istr, str);
}
}
|
neglecting the actual implementation, is this the correct apporach?
2nd problem:
i am unsure as to what exactly i have to redefine and in what manner. can i modify the argument str from within this function?
Any advice, hints, tips are much appreciated
thanks, jaqq