数组中存在的数字

时间:2019-08-12 16:49:30

标签: c++ vector file-io

我有一个包含几行实数的文本文件,我应该计算每行中存在多少个数字。我的想法是用for循环在未知大小的数组内插入每一行。但是我不知道如何计算每行文本中每个数组中存在多少个数字。我该怎么办?谢谢

#define n 1000
#include <fstream>
#include <iostream>

using namespace std;
int main(){
  ifstream in("input.txt");
  ofstream out("output.txt");
  for(int i=0; i<100; i++){
     double* vec= new doule[n];
     for(int j=0; j<n; j++)
        in >> vec[j];
  }
}

1 个答案:

答案 0 :(得分:1)

您可以简单地通过计算文件中每一行允许输入的数量来做到这一点。首次使用

#include <iostream>
#include <vector>
using namespace std;
int main(){
    std::string myLine;
    std::vector<int> vec;
    ifstream in("input.txt");
    while(getline(in, myLine)){
        counter = 0;
        while(canTakeInput()){
            counter++;
        }
        vec.push_back(counter);
    }
}

有关如何解析或接受输入的想法,我将使用this链接