Copyright: from: Jihome https://blog.csdn.net/jihome/article/details/88918637
Sort function is very easy to use, C ++ comes with a sort function is, and that is the sort function, the following is a summary of the use of the sort function, in order to be more efficient programming, to understand some of the built-in functions are very useful.
Reference blog: TX .
The source code (specific note):
/**sort函数的使用*/
#include <iostream>
#include <algorithm>//sort函数要包含的头文件
#include <cstring>
using namespace std;
int main(){
int a[5]={2,1,3,4,5};
string s="hello world";
int i;
sort(a,a+5,less<int>());//对Int型的sort greater(降序) less(升序)
for(i=0;i<5;i++){
cout<<a[i]<<" ";
}
sort(s.begin(),s.end());//字符串的排序(正序)
cout<<endl<<s<<endl;
sort(s.rbegin(),s.rend());//逆序(反向迭代器实现)
cout<<s<<endl;
string str[3]; //字符串之间的比较
for(i=0;i<3;i++){
getline(cin,str[i]);
}
sort(str,str+3);
for(i=0;i<3;i++){
cout<<str[i]<<endl;
}
return 0;
}