怎么实现将数组按对象某一属性值的首字母排序
展开全部
#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
struct A
{
int a;
int b;
int c;
};
bool comp(const A &p, const A &q)
{
return p.a > q.a;
}
int main()
{
fstream fin("1.txt");
vector<A> T;
A temp;
while (fin >> temp.a >> temp.b >> temp.c)
{
T.push_back(temp);
}
fin.close();
sort(T.begin(), T.end(), comp);
auto ite = T.begin();
auto end_ite = T.end();
while (ite != end_ite)
{
cout << (*ite).a << ends << (*ite).b << ends << (*ite).c << endl;
ite++;
}
system("pause");
return 0;
}
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
struct A
{
int a;
int b;
int c;
};
bool comp(const A &p, const A &q)
{
return p.a > q.a;
}
int main()
{
fstream fin("1.txt");
vector<A> T;
A temp;
while (fin >> temp.a >> temp.b >> temp.c)
{
T.push_back(temp);
}
fin.close();
sort(T.begin(), T.end(), comp);
auto ite = T.begin();
auto end_ite = T.end();
while (ite != end_ite)
{
cout << (*ite).a << ends << (*ite).b << ends << (*ite).c << endl;
ite++;
}
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询