Should operator>> clear a container?
c++, iostream, refactoring
Solution
The extraction operators provided by the standard library all replace the target variable's contents.
Including `std::string`, which is a container type, like your examples.
Problem
I'm refactoring some code that reads containers of integers from files. I've moved the common code into stream extractors like these: ``` std::istream &operator>>(std::istream &in, std::vector<int> &list); std::istream &operator>>(std::istream &in, std::map<int, std::vector<int>> &graph); ``` My question is whether these functions should clear the containers before writing, or just insert/append data to them. (In my particular case, it doesn't matter, but in the interest of code reuse, I'd like to know.) Is there a convention in C++ for these situations? I'm trying to code according to the Principle of Least Surprise.