c++ - 如何使用 vector 和对象构造函数初始化对象 vector ?

标签 c++ vector c++11 initialization

如何初始化 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());

关于c++ - 如何使用 vector 和对象构造函数初始化对象 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577497/

相关文章:

c++ - std::array initializer list 在初始化列表中初始化

c++ - std::list 的循环迭代

c++ - 简单参数包扩展 : expected ';'

c++ - 数组下标错误的类型 'int[int]'无效

c++ - 如何在 C++ 中获取进程的起始地址/基地址?

math - 确定一些向量有何不同

c++ - 与运行时相比,为什么三元运算符在编译时的工作方式不同?

c++ - 如何使用Codelite 11.0.0编译静态库?

c++ - 将类的实例插入 vector C++

c++ - 使用 size_t 值反向遍历 vector