【发布时间】:2017-04-24 13:35:22
【问题描述】:
我想比较矩阵每一行的元素,从最小到最大。 但是我不想打印排序后的数组我想打印原始位置。
0 11.80 79.34 78.23
11.80 0 65.23 45.19
79.34 65.23 0 90.27
78.23 45.19 90.27 0
在这个矩阵的第一行,我想打印 1、2、4、3
到目前为止我的代码:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
string dummy;
double myArray[4][4];
int i;
int j;
int y;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
infile >> myArray[i][j];
if (!infile) {
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int x = myArray[i][j];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if(myArray[i][j] >= x) {
x = j;
}
else {
x = j + 1;
}
}
cout << x << endl;
}
return 0;
}
【问题讨论】:
-
你的问题是?
-
如何打印排序好的位置
-
看看std::map。您可以将数组值映射到它们的索引。在地图填满一行后,您可以将地图从
begin()输出到end(),打印映射的索引。如果可能有多个具有相同值的数组元素,则应使用std::multimap。 -
我的问题是我不知道如何将我的矩阵(矩阵中的每一行作为自己的向量)初始化为向量,因为我必须从外部 .dat 文件中获取这个矩阵。 .