- 1.对string排序
#include<bits/stdc++.h>
using namespace std;
vector<string>a;
bool compare1(string a,string b){
return a>b;
}//升序
bool compare2(string a,string b){
return a.size()>b.size();
}//字符串的长度
int main(){
int n;cin>>n;
string x;
for(int i=0;i<n;i++){
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end(),compare1);
for(auto s:a){
cout<<s<<endl;
}
return 0;
}
- 2.对结构体排序
struct Dog{
int age;
int weight;
string sex;
};
bool cmp(struct Dog a,struct Dog b){
if(a.sex==b.sex){
if(a.age==b.age){
return a.weight>b.weight;
}
return a.age>b.age;
}
return a.sex>b.sex;
}
3.贪心算法-连续问题
www我怎么现在才知道!!
java和c++等字符串连在一起都是可以直接相加的
#include<bits/stdc++.h>
using namespace std;
vector<string>a;
bool compare(string a,string b){
return (a+b)>(b+a);//www我怎么现在才知道
}
int main(){
int n;cin>>n;
string x;
while(n--){
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end(),compare);
for(auto s:a){
cout<<s;
}
return 0;
}