c++ - how to fill std::vector from istream using standard stl algorithms -


there old legacy code fills vector istream , objects within vector accept in ctor string raw data.

typedef std::vector<myclass*> my_array;  std::istream& operator >> (std::istream& s, my_array& arr) {    if (s) {       std::istream_iterator<std::string> i_iter = s;       for(++i_iter; !s.eof(); arr.push_back(new myclass(*i_iter++)));    }    return s; } 

where myclass ctor looks like:

myclass(const std::string& data); 

do see way avoid writing operator>> or other functions , use (?) standard algorithm fill container constructed objects? replace pointers on values within container emplace contructing.

by way, code compiled vc10 doesn't work properly, looks infinite loop when i'm stepping on for. istream (really ifstream there) small file ~200 lines of text

you use std::transform. code requires c++11, if won't work you, change lambda factory method , alias declaration typedef:

using it_type = std::istream_iterator<std::string>; std::transform(it_type{std::cin}, it_type{},                std::back_inserter(a), [](const auto& a) { return new myclass(a); }); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -