01/10/2018, 10:09

Cách sắp xếp bản ghi (Struct) trong C++

Như câu hỏi ạh T.T em muốn sắp xếp bản ghi mà không biết làm thế nào đây (sắp xếp bằng qsort áh)
Mong mọi người tư vấn giúp em ạh

Tin Tin viết 12:18 ngày 01/10/2018

Chào mọi người !! Em vừa test đc rồi ^^ Nếu ai chưa biết như em có thể vào check nhe ^^

#include <bits/stdc++.h>
using namespace std;
const int N=1e5;
struct ds {
	int mot,hai;
};
ds a[N];
int n;
bool xet(const ds x , const ds y) {
	return x.mot<y.mot;
}

int main() {
	ifstream f("test.inp");
	ofstream g("test.out");
	f>>n;
	for(int i=1;i<=n;i++) f>>a[i].mot>>a[i].hai; 
	sort(a+1,a+1+n,xet);
	for(int i=1;i<=n;i++) g<<a[i].mot<<" "<<a[i].hai<<endl;
	f.close();
	g.close();
}
Bài liên quan
0