要读取txt文件并从第二列中找到其最小值的第一列值
to read a txt file and find its minima from second column for a given value of first column
i有一个数据文件,其中第一列从-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
使用关联容器跟踪多个最小值。例如,对此任务的订购std::map<int, double>
似乎很方便:
- http://en.cppreference.com/w/cpp/container/map
解决方案的一般结构看起来像这样:
#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
}
供参考:
- https://en.wikipedia.org/wiki/associative_arrays
- https://en.wikipedia.org/wiki/associative_containers
- 按行读取文件
- http://en.cppreference.com/w/cpp/container/map
- 如何查找C STD中的给定密钥是否存在
- http://de.cppreference.com/w/cpp/container/map/find
- C 打印出地图值
这也可以在读取数据时简单,顺序完成,而无需将其存储在任何容器中。因为将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";
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 要读取txt文件并从第二列中找到其最小值的第一列值
- 有没有一种无分支方法可以快速找到两个双精度浮点值的最小值/最大值
- 在 C++ 中,在矩阵中搜索最小值和最大值以及不同行或列的最快方法是什么
- 如何将用户输入的最小值限制为流大小减去一位
- 从向量向量中的列中获取最小值和最大值
- 使用 ifstream 读取列中随机生成的数字的文本文件,并从列表中查找最小值和最大值
- C++在二维数组中的每一行和每一列中查找最小值和最大值
- 查找列中的最大值和最小值