How to initialize a vector of objects using both a vector and an object constructor?

How to initialize std::vector<std::ifstream> from an existing std::vector<std::string> , which are the names of the files that are intended to be opened?

Without vector initialization, I can do this using

 std::vector<std::string> input_file_names; // Populate the vector with names of files that needs to open. // ... std::vector<std::ifstream> input_files_; for (auto const & input_file_name : input_file_names) { input_files_.emplace_back(input_file_name); } 
+4
source share
1 answer

In C ++ 11, the std::ifstream will accept the std::string parameter as the parameter. A string that along with the copy constructor is std::vector , and this should work:

 std::vector<std::string> filenames; std::vector<std::ifstream> files(filenames.begin(), filenames.end()); 
+14
source

All Articles