【问题标题】:to read a txt file and find its minima from second column for a given value of first column读取 txt 文件并从第二列中为第一列的给定值找到其最小值
【发布时间】:2018-09-14 21:22:11
【问题描述】:

我有一个数据文件,其中第一列从 -180 到 +180 不等,宽度为 5,而第二列是一些相应的值。我需要从第二列中找到一个对应值的最小值,首先说-180,打印出来,然后从第二列中找到-175的最小值,然后打印,等等......当我只有一列时,我有代码来查找最小值.我如何包含这个循环

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
    ifstream myfile("ma3.txt");
    if(myfile.is_open())
    {       
      int arrSize=0.0;
      double arr[2000];
      double min=0;

      while(true)
      {
         double x,y;
         myfile>>x;
         if(myfile.eof()) break;
         arr[arrSize++]=x;        
      }
      //for(int i=0; i<arrSize; ++i)
      //   cout<<arr[i]<<"";
      //cout<<endl;

      min=arr[0];
      for(int i=0;i<arrSize;i++)
      {
         {          
             if(arr[i]<min)
             {
                min=arr[i];
             }
         }
      }

      cout<<"Smallest element:";
      cout<<min;
      cout<<endl;
    }
    else
    {
       cout<<"Unable to open file";
    }

    return 0;
} 

输入数据:

-180 431.845 
-180 434.337 
-180 436.819 
-180 439.289 
-180 469.936 
-180 472.152 
-180 474.343 
-180 476.509 
-180 478.649 
-180 480.761 
-180 482.846 
-180 484.902 
-180 486.929 
-180 488.926
-175 387.566 
-175 384.891 
-175 382.216 
-175 379.541 
-175 376.868 
-175 374.197 
-175 371.53 
-175 368.867 
-175 366.209 
-175 363.557 
-175 360.912 
-175 358.275 
-175 355.648 
-175 353.03 
-175 350.422 
-175 347.826 
-175 345.243 
-175 342.673 
-175 340.117 
-175 337.576 
-175 335.05 
-175 332.541 
-175 330.05 
-175 327.576

【问题讨论】:

  • 你能提供一个输入的例子吗?
  • 是否允许使用其他容器,例如地图和矢量?
  • 如何附加数据文件。输入如-180 431.845 -180 434.337 -180 436.819 -180 439.936 -180 472.152 -180 474.343 -180 476.509 -180 478.649 -180 480.761 -180 482.846 -180 484.902 -180 484.929 -180 486.929 -180 486.929 -180 486.929 -180 486.929 -180 484.929 -180 486.929 -180 486.566 175 384.891 -175 382.216 -175 379.541 -175 376.868 -175 374.175 -175 371.53 -175 368.867 -175 366.209 -175 363.557 -175 360.912 -175 358.275 -175 355.648 -175 355.648 -175 355.648 -175 353.03 -175 353.03 -175 350.422 -175 347.826 -175 345.243 -175 345.243 -175 342.673 -175 -175 340.117 -175 337.576 -175 335.05 -175 332.541 -175 330.05 -175 327.576
  • @ThomasSablik 它实际上是一个向量,我只是为了方便起见将其命名为数组。
  • @MasoomSingh 不,您使用的是数组,而不是向量。要使用容器,您需要相应的头文件,例如#include &lt;vector&gt;#include &lt;map&gt;

标签: c++


【解决方案1】:

这也可以在读取数据时简单地按顺序完成,而无需将其存储在任何容器中。因为 x 的值被组合在一起,所以当第一列发生变化时,只需打印出最小值。像这样的东西(未经测试):

bool first = true;
int x = 0;
double y = 0;
double minY = 0;
int prevX = 0;


while (myfile >> x >> y)
{
    if (x == prevX)
    {
        if (y < minY)
            minY = y;
    }
    else
    {
        if (!first)
            cout << prevX << " smallest element: " << minY << "\n";

        minY = y;
        prevX = x;
        first = false;
    }
}

if (!first)
    cout << prevX << " smallest element: " << minY << "\n";

【讨论】:

  • 它完全按照我的意愿工作,我什至理解数字限制的概念。非常感谢。你拯救了我的一天。
【解决方案2】:

使用关联容器来跟踪多个最小值。例如,一个有序的std::map&lt;int, double&gt; 似乎对这个任务很方便:

解决方案的一般结构可能如下所示:

#include <iostream>
#include <map>
int main() {
    std::map<int, double> mins;
    int x1;
    double x2;
    // TODO: read from file instead
    // TODO: read lines using `std::getline()` 
    //       and parse values using `std::stringstream` instead
    while (std::cin >> x1 >> x2) {
        // check if x1 is already being tracked
        if (mins.find(x1) != mins.end()) {
            // unknown key: just insert current value
            mins[x1] = x2;
        }
        else {
            // TODO: update value as needed
        }
    }
    // TODO: print key-value pairs in map
}

供参考:

【讨论】:

  • 你能解释一下你在 if 循环中做了什么吗?
  • @MasoomSingh 容器mins 最初是空的。因为您需要根据您是否已经知道密钥来做另一件事,所以您需要条件(find 检查密钥是否已知)。然后你要么插入当前值,要么更新最小值。
  • 很抱歉,我不知道您提到的“密钥”一词。有没有其他方法可以做到这一点?否则我必须先了解这一点:-(
  • @MasoomSingh 这是来自associative data structures(又名键值存储)的术语。在您的情况下, key 是第一列值, value 是第二列值。您应该真正尝试理解这一点,因为它对于这里发生的事情至关重要。
  • 那么 else 中的条件应该是什么?
猜你喜欢
  • 1970-01-01
  • 2020-05-13
  • 2022-01-17
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2014-12-07
相关资源
最近更新 更多