Why use boost for tasks that can be achieved with C ++ std? Just use istream_iterator with insert_iterator. To do this, you must define in the operators << and '→' in the std namespace for your pair<string,string> . Something like that:
namespace std { // I am not happy that I had to put these stream operators in std namespace. // I had to because otherwise std iterators cannot find them // - you know this annoying C++ lookup rules... // I know one solution is to create new type inter-operable with this pair... // Just to lazy to do this - anyone knows workaround? istream& operator >> (istream& is, pair<string, string>& ps) { return is >> ps.first >> ps.second; } ostream& operator << (ostream& os, const pair<const string, string>& ps) { return os << ps.first << "==>>" << ps.second; } }
And use:
std insert iterator:
std::map<std::string, std::string> mps; std::insert_iterator< std::map<std::string, std::string> > mpsi(mps, mps.begin());
std istream iterator:
const std::istream_iterator<std::pair<std::string,std::string> > eos; std::istream_iterator<std::pair<std::string,std::string> > its (is);
Reading:
std::copy(its, eos, mpsi);
Writing (bonus):
std::copy(mps.begin(), mps.end(), std::ostream_iterator<std::pair<std::string,std::string> >(std::cout, "\n"));
Working example in ideone
source share