题目1190:大整数排序 C++实现

有的时候,自己写过的代码,自己再看时就不认识了。

有必须要记录下来。把数的排序转换为字符串的排序。

原来用sort函数也可以对字符串排序。对C++的库函数不是很熟悉~~~

sort(a,a+n);   //两个参数分别为待排序数组的首地址和尾地址

当然还可以传第三个参数,自定义的比较函数。

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

bool pare(string a,string b){
if(a.size() > b.size())
return false;
else if(a.size() == b.size()){
if(a.compare(b) < 0)
return true;
else
return false;
}
else
return true;
}
int main(){
string *str ;
int n;
while (cin>>n)
{
str = new string[n];
for(int i=0; i<n; i++){
cin>>str[i];
}
sort(str,str+n,pare); //对字符串进行排序。
for(int i=0; i<n; i++){
cout<<str[i]<<endl;
}
}
return 0;
}



原文地址:https://www.cnblogs.com/love533/p/2430915.html