临沂做网站公司哪家好,做网站的软件淘汰史,用ftp做网站,律所网站建设国队男子接力赛最近在刷ACM经常用到排序#xff0c;以前老是写冒泡#xff0c;可把冒泡带到OJ里后发现经常超时#xff0c;所以本想用快排#xff0c;可是很多学长推荐用sort函数#xff0c;因为自己写的快排写不好真的没有sort快#xff0c;所以毅然决然选择sort函数用法1、sort函数可… 最近在刷ACM经常用到排序以前老是写冒泡可把冒泡带到OJ里后发现经常超时所以本想用快排可是很多学长推荐用sort函数因为自己写的快排写不好真的没有sort快所以毅然决然选择sort函数用法1、sort函数可以三个参数也可以两个参数必须的头文件#include algorithm和using namespace std;2、它使用的排序方法是类似于快排的方法时间复杂度为n*log2(n)3、Sort函数有三个参数第三个参数可不写1第一个是要排序的数组的起始地址。2第二个是结束的地址最后一位要排序的地址3第三个参数是排序的方法可以是从大到小也可是从小到大还可以不写第三个参数此时默认的排序方法是从小到大排序。两个参数用法#include
#include
int main()
{int a[20]{2,4,1,23,5,76,0,43,24,65},i;for(i0;i20;i )couta[i]endl;sort(a,a 20);for(i0;i20;i )couta[i]endl;return 0;
}
输出结果是升序排列。两个参数的sort默认升序排序三个参数// sort algorithm example
#include // std::cout
#include // std::sort
#include // std::vectorbool myfunction (int i,int j) { return (ibool myfunction2 (int i,int j) { return (ij); }//降序排列struct myclass {bool operator() (int i,int j) { return (ij);}
} myobject;int main () {int myints[8] {32,71,12,45,26,80,53,33};std::vector myvector (myints, myints 8); // 32 71 12 45 26 80 53 33// using default comparison (operator ):std::sort (myvector.begin(), myvector.begin() 4); //(12 32 45 71)26 80 53 33// using function as compstd::sort (myvector.begin() 4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)//std::sort (myints,myints 8,myfunction);不用vector的用法// using object as compstd::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)// print out content:std::cout myvector contains:;for (std::vector::iterator itmyvector.begin(); it!myvector.end(); it)//输出std::cout *it;std::cout \n;return 0;
}
string 使用反向迭代器来完成逆序排列#include
using namespace std;
int main()
{string str(cvicses);string s(str.rbegin(),str.rend());cout s endl;return 0;
}
//输出sescivc来源CSDN - 浅然言而信https://blog.csdn.net/w_linux/article/details/76222112