【发布时间】:2016-11-22 14:55:14
【问题描述】:
我有一个数据列表(4 列),我想按某列排序。它是从文件读入到二维向量中的。我使用了 std::sort 方法并编写了我的比较函子。程序编译并运行,但是当我打印前 10 个元素时,它没有排序,但肯定不同于它添加到 2D 向量的顺序。
代码如下:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
typedef vector<double> Row;
typedef vector<Row> Matrix;
bool sortByFourthColumn(const Row& row1, const Row& row2){
return (double) row1[3] < (double) row2[3];
}
int main(){
std::ifstream infile;
infile.open("Test8_Output.txt");
double x,y,z,E;
char line[200];
int line_count=0;
ofstream outfile;
outfile.open("WO_crp.txt");
if (infile.is_open()){
while (!infile.eof()){
infile.getline(line,170);
if (line[0] != '%'){
outfile<<line<<"\n";
line_count++;
}
else{
}
}
Matrix data(line_count,Row(4));
outfile.close();
std::ifstream myfile;
myfile.open("WO_crp.txt");
int i = 0;
while(myfile >> x >> y >> z >> E){
data[0][i] = x;
data[1][i] = y;
data[2][i] = z;
data[3][i] = E;
i++;
}
myfile.close();
std::sort(data.begin(), data.end(), sortByFourthColumn);
for (int u = 0; u <20; u++){
cout << setprecision(5) << data[0][u] << "\t" << setprecision(5)<< data[1][u] << "\t" << setprecision(5)<< data[2][u] << "\t" << setprecision(5)<< data[3][u] << endl;
}
}
else{
cout << "Error: File is invalid.\n";
}
return(0);
}
编辑 - 输入文件的样例:
编辑 2 - 在 Matrix data(4,Row(line_count)); 中交换 4 和 line_count
% Model: CDS_Test8.mph
% Version: COMSOL 5.2.0.220
% Date: Jul 13 2016, 14:33
% Dimension: 3
% Nodes: 86183
% Expressions: 1
% Description: Electric field norm
% Length unit: m
% x y z es.normE (V/m)
0.13774675805195374 0.05012986567931247 0.20735 67.35120820901535
0.13870000000000005 0.04957489750396299 0.20735000000000003 102.8772500513651
0.13870000000000002 0.050800000000000005 0.20735 87.56008679032011
0.13792733849817027 0.050131465727838186 0.20801419247484804 73.55192534768238
0.13674627634411463 0.04992349737428063 0.20735 63.23018910026428
0.13750191177019236 0.0508 0.20735000000000003 67.26176884022838
0.13827743496772454 0.05193409099097887 0.20734999999999998 73.35474409597487
0.13803618792088135 0.05134931748395268 0.20841988134890965 75.3712126982815
0.13905949760011943 0.05141879754884912 0.20734999999999998 83.70739713476813
0.13896970815034013 0.05092428105421264 0.208142746399683 84.73571510992372
0.1390220807917094 0.0501245422629353 0.20817502757007986 85.57119242707628
0.13944867847480893 0.05161480113017738 0.2081969878426443 89.65643851109644
以此类推,再写 87k 行左右。
【问题讨论】:
-
我很困惑。 std::sort 不负责排序(因此交换)?为什么要有交换功能?
-
使用
Matrix data(4,Row(line_count)),您声明了一个包含Row(line_count)类型的4 个元素的向量(line_count 双倍的向量)。您应该在程序的其余部分中将 4 与 line_count 等交换。 -
感谢您发现这个@Bob__。我不明白原因,但我实施了更改。输出现在看起来“更多”排序。然而事实并非如此。会不会是精度问题?
-
你能提供一个输入文件的例子吗?您是否复制该文件只是为了摆脱 cmets 行(以 '%' 开头的行)?
-
索引时您仍然可以反转尺寸。既然
line_count是第一个索引,那么您的循环索引i或u也必须是第一个索引。
标签: c++ sorting vector 2d-vector