小编use*_*063的帖子

从文件中读取整数到C++中的向量

我试图读取存储在从文本文件的单独行中的未知数量的双值到称为的向量rainfall.我的代码不会编译; 我收到了no match for 'operator>>' in 'inputFile >> rainfall'while循环行的错误.我理解如何从一个文件读入一个数组,但我们需要使用矢量这个项目,我没有得到它.我感谢您在下面的部分代码中提供的任何提示.

vector<double> rainfall;    // a vector to hold rainfall data

// open file    
ifstream inputFile("/home/shared/data4.txt");

// test file open   
if (inputFile) {
    int count = 0;      // count number of items in the file

    // read the elements in the file into a vector  
    while ( inputFile >> rainfall ) {
        rainfall.push_back(count);
        ++count;
    }

    // close the file
Run Code Online (Sandbox Code Playgroud)

c++ vector

5
推荐指数
4
解决办法
4万
查看次数

标签 统计

c++ ×1

vector ×1