按身份证中年龄降序排序
int cmp (string a,string b)
{
string a1=a.substr(6,8);
string b1=b.substr(6,8);
if(a1==b1) return a>b;else return a1>b1;
}
sort函数中添加,cmp即可
#include <bits/stdc++.h>
using namespace std;
int cmp (string a,string b)
{
string a1=a.substr(6,8);
string b1=b.substr(6,8);
if(a1==b1) return a>b;else return a1>b1;
}
int main()
{//["241281198302271034","251281199301271011","261281198402071013","231281202005301017"]
//vector<int >isi(10,1);
string s="241281198302271034",s2="251281199301271011";//cout<<s.substr(6,8);//从678
string a[]={"241281198302271034","251281199301271011","261281198402071013"};
vector<string> ids(a,a+3);sort(ids.begin(),ids.end(),cmp);
cout<<"[";
for(int i=0;i<3;i++)
{
cout<<"\"";
cout<<ids[i];
cout<<"\"";
if(i<2)cout<<",";
}
cout<<"]";
//leetcode模式 自定义函数放在类外 vector<string> k(a);return k
}