2016-09-28 136 views
0

我需要帮助。我正在编写这个程序,需要计算.csv文件中两个整数之间的差异。找出差异是很容易的,但是当我列出差异列表时,它们没有特定的顺序。我想挑选出具有最高价值的差异和特别的结果。我会怎么做呢?Cout按降序排列C++

double diff; 
    int astate; 

    string line8; 
    ifstream myfile8 ("elect12.csv"); 
    //cout << endl << "Total Popular Votes for Other Candidates: "; 
    while (getline (myfile8, line)) 
    { 
     istringstream iss(line); 
     int a, b, c, d, e; 
     if (!(iss >> a >> b >> c >> d >> e)) //{ break; } 
     diff = (a-b); 
     diff = diff/d; 
     diff = diff*100; 
     astate = diff;   
     /*cout << "Obama's best state was " << line8 << ", where he won by " << diff << " points." << endl;*/ 
     cout << astate << endl; 

将差值换算成百分比。现在,& b之间的所有差异正在返回。我如何才能返回最大的差异?

对不起,如果这没有意义。

+0

cout内循环是有罪的 –

+0

你必须保存最大的差异yoursef –

+0

并在外侧的循环,显示它 –

回答

2

比较与以往最大的不同电流差(存储在初始化变量astate),如果它是比以前更大的规模最大,然后分配。循环后打印astate的值。

+1

@DominickAlcantara类似的东西,但也许'diff> astate'。 –

+0

是否意味着使用 if(astate> diff)? –