C++ 从一个文件中查找多个和最有效的方法是什么?

C++ 从一个文件中查找多个和最有效的方法是什么?,c++,scalability,C++,Scalability,假设我有一个包含多行3列的文件: 3 1 3 1 2 3 4 5 6 . . . 目标是找到每列的总和。解决方案很简单:为总和生成3个变量,然后再生成3个临时变量 然而,这个解决方案不能很好地扩展。如果有6列呢?然后我要做12个变量。还有其他方法,比如只生成一个count变量和一个temp变量,并使用count的模将temp变量添加到正确的和中。但这似乎是一个黑客 有没有一个更好的方式来做这个或C++标准库?这意味着什么? < P> Pseudocode: Open FilePointer (

假设我有一个包含多行3列的文件:

3 1 3
1 2 3
4 5 6
. . .
目标是找到每列的总和。解决方案很简单:为总和生成3个变量,然后再生成3个临时变量

然而,这个解决方案不能很好地扩展。如果有6列呢?然后我要做12个变量。还有其他方法,比如只生成一个count变量和一个temp变量,并使用count的模将temp变量添加到正确的和中。但这似乎是一个黑客

有没有一个更好的方式来做这个或C++标准库?这意味着什么?

< P> Pseudocode:

Open FilePointer (readonly)
create a queue of ints.
create a ptr to queue.
read in a row.
tokenize on space
walk array and push onto ptr the value = value + variable
shift ptr,     ptr = ptr->next()
at end of row, shift ptr back to head.
do while not EOF.

walk queue last time, outputing the values.
while(ptr != nullptr) {  cout << ptr->value; }
打开文件指针(只读)
创建一个整数队列。
创建要排队的ptr。
连续阅读。
在空间上标记化
遍历数组并将value=value+变量推到ptr上
移位ptr,ptr=ptr->next()
在行的末尾,将ptr移回头部。
不要用EOF。
上次遍历队列,输出值。
while(ptr!=nullptr){cout value;}
您可以使用动态调整列数。向量的每个元素对应于一列的和

您可以这样做:

#include <iostream>   
#include <string>      // for using getline()
#include <fstream>     // for file reading 
#include <sstream>     // for line parsing with stringstream 
#include <vector>      // for vectors
#include <algorithm>   // for the output trick

using namespace std; 

int main()
{
   vector<int> sum;              // intiial size is 0
   ifstream ifs("test.txt");
   string line; 

   while (getline(ifs, line)) {  // read file line by line; 
     stringstream ss(line);    // and parse each line
     int input; 
     for (int i = 0; ss >> input; i++) {   // read columns (i counts them)
        if (i >= sum.size())       // if there is a new column 
            sum.resize(i+1);              // resize the vector
        sum[i]+=input;             // in any case update sum of the column  
     }
   }                
       // when it's finished, just output the result
   copy(sum.begin(), sum.end(), ostream_iterator<int>(cout, "; ")); 
   cout << endl; 
}
它将显示:

14; 23; 28; 23; 28;

为什么不使用一个名为sum的变量和一个名为temp的变量呢。基本纲要:

Initialize sum to 0;
while there are more lines:
    Read 1 line of input till \n is found
        While line has more inputs:
            read each number out of it (using temp) 
            sum += temp
        Next Number
        print out sum and reset it to 0
    Next Line

使用
向量
。如果使用队列,则每行从头部开始,然后逐步移动到
\n
字符,然后将指针移回头部。然后,当您完成所有操作后,只需在队列中走动一次即可获得输出。我觉得比向量开销小。@CoryNelson你能解释一下向量的用法吗?@joshualan制作一个包含你读入的每列的sum和temp变量的sum
vector
structs
。或者两个独立的
向量,一个用于求和,一个用于临时变量。哦,好的。我不确定。我想你可以在流中阅读来优化它,但我认为它会工作得相当好。我是说,至少对我来说是有道理的。我很想知道为什么我的不是一个好主意。这个解决方案只需要大约3个变量就可以完成工作,并且可以扩展到所有大小的输入,甚至可以更改每行值的数量!
Initialize sum to 0;
while there are more lines:
    Read 1 line of input till \n is found
        While line has more inputs:
            read each number out of it (using temp) 
            sum += temp
        Next Number
        print out sum and reset it to 0
    Next Line