如何使用矢量和对象构造函数初始化对象的向量

How do I initialize a vector of objects using both vector and the objects constructor?

本文关键字:对象 构造函数 初始化 向量 何使用      更新时间:2023-10-16

如何初始化 std::vector<std::ifstream>现有std::vector<std::string>哪些是要打开的文件的名称?

无需初始化vector,我可以使用

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);
}

在 c++11 中,std::ifstream构造函数将std::string作为参数。 将其与std::vector复制构造函数一起字符串,这应该有效:

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