我正在尝试按升序对文件内容进行双排序
我的输入文件包含例如以下几行:
105 350 4.41386e-06 4.41386e-06
115 300 4.58965e-06 4.58965e-06
15 150 1.6457e-06 1.6457e-06
255 550 5.33661e-05 5.33661e-05
25 150 3.21907e-06 3.21907e-06
35 550 2.57952e-05 2.57952e-05
45 150 1.78332e-06 1.78332e-06
Run Code Online (Sandbox Code Playgroud)
我希望我的输出文件具有以下内容:
15 150 1.6457e-06 1.6457e-06
25 150 3.21907e-06 3.21907e-06
35 550 2.57952e-05 2.57952e-05
45 150 1.78332e-06 1.78332e-06
105 350 4.41386e-06 4.41386e-06
115 300 4.58965e-06 4.58965e-06
255 550 5.33661e-05 5.33661e-05
Run Code Online (Sandbox Code Playgroud)
由于我只是 C++ 编码的初学者,如果尝试过这些行来完成该任务:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream textfile( "Limit.txt" );
std::string text_input;
std::vector< std::string > sort_vec;
std::ofstream outfile1;
TString outfile_name1 = "Limits.txt";
if ( textfile.is_open() )
{
outfile1.open( outfile_name1 );
while ( std::getline( textfile,text_input ) )
{
sort_vec.push_back(text_input);
}
textfile.close();
}
std::sort( std::begin( sort_vec ), std::end( sort_vec ), std::less<string>() );
for ( const auto & e : sort_vec )
{
outfile1 << e << "\n";
}
outfile1.close();
}
Run Code Online (Sandbox Code Playgroud)
但我无法得到我想要的结果。
请帮我解决一下。
由于每行中的空格和数据长度不同,将行作为字符串进行字典顺序排序可能会产生意外结果或完全错误。
因此,我建议将这些行分成 4 部分,使用>>运算符使用正常的 IO 提取功能非常简单。
一行数据可以存储在一个结构体中。这在某种程度上是直观的、可读的和可理解的。为了存储整个列表,我们可以使用std::vector这样的结构体。
除了使用 struct 之外,我们还可以使用std::tupleJarod42 所建议的 a。Astd::tuple有比较运算符,允许稍后对一行进行超简单的排序。
反正。让我们从一个结构开始,它具有其他可能性,如下所示。
std::vector上述结构体的avector(我们将其称为数据库)这可以通过多种方式进行编码。看看下面的潜在例子:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
struct Data {
int i1{};
int i2{};
double d1{};
double d2{};
};
int main() {
// Open the source textfile and check, if it could be opened
std::ifstream textfile("Limit.txt");
if (textfile) {
// Here we will store all data
std::vector<Data> database{};
// Temporary storage for 1 line
Data data{};
// Read all lines and add data to the database
while (textfile >> data.i1 >> data.i2 >> data.d1 >> data.d2)
database.push_back(data);
// Sort database and use a lambda to do it as wished
std::sort(database.begin(), database.end(), [](const Data& d1, const Data& d2) {
return d1.i1 == d2.i1 ? (d1.i2 == d2.i2 ? (d1.d1 == d2.d1 ? d1.d2 < d2.d1 : d1.d1 < d2.d1) : d1.i2 < d2.i2) : d1.i1 < d2.i1; });
// Debug output
for (const Data& d : database)
std::cout << std::left << std::setw(4) << d.i1 << std::setw(4) << d.i2 << std::setw(12) << d.d1 << d.d2 << '\n';
}
else std::cerr << "\n*** Error! Could not open source file\n\n";
}
Run Code Online (Sandbox Code Playgroud)
但这还不是全部。
如您所知,C++ 是一种面向对象的语言。我们可以将数据与函数一起存储在结构中(结构是一个类),并对这些数据进行操作。
因此,我们可以添加输入和输出函数以及排序所需的小于运算符。
那么main函数就会明显简单很多。
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
struct Data {
int i1{};
int i2{};
double d1{};
double d2{};
// Methods / operators
// Input
friend std::istream& operator >> (std::istream& is, Data& data) {
return is >> data.i1 >> data.i2 >> data.d1 >> data.d2;
}
// Output
friend std::ostream& operator << (std::ostream& os, const Data& d) {
return os << std::left << std::setw(4) << d.i1 << std::setw(4)
<< d.i2 << std::setw(12) << d.d1 << d.d2;
}
// Comparison
bool operator < (const Data& other) {
return i1 == other.i1 ? (i2 == other.i2 ? (d1 == other.d1 ? d2 < other.d1 : d1 < other.d1) : i2 < other.i2) : i1 < other.i1;
}
};
int main() {
// Open the source textfile and check, if it could be opened
std::ifstream textfile("Limit.txt");
if (textfile) {
// Here we will store all data
std::vector<Data> database{};
// Temporary storage for 1 line
Data data{};
// Read all lines and add data to the database
while (textfile >> data)
database.push_back(data);
// Sort database
std::sort(database.begin(), database.end());
// Debug output
for (const Data& d : database)
std::cout << d << '\n';
}
else std::cerr << "\n*** Error! Could not open source file\n\n";
}
Run Code Online (Sandbox Code Playgroud)
优点:易懂、简单。良好的排序控制。缺点:排序标准复杂