网站建设备案流程,安卓手机优化大师官方下载,网站建设logo要什么格式,开发公司资料员岗位职责及工作内容1、sort (first, last) #xff1a; 对容器或普通数组中 [first, last) 范围内的元素进行排序#xff0c;默认进行升序排序。
2、stable_sort (first, last)#xff1a; 和 sort() 函数功能相似#xff0c;不同之处在于#xff0c;对于 [first, last) 范围内值相同的元素…1、sort (first, last) 对容器或普通数组中 [first, last) 范围内的元素进行排序默认进行升序排序。
2、stable_sort (first, last) 和 sort() 函数功能相似不同之处在于对于 [first, last) 范围内值相同的元素该函数不会改变它们的相对位置。
3、stable_sort() 函数是基于归并排序实现的
4、sort() 函数是基于快速排序实现的
sort()函数的使用例子
#include iostream // std::cout
#include algorithm // std::stable_sort
#include vector // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {return (i j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
public:bool operator() (int i, int j) {return (i j);}
};int main() {std::vectorint myvector{ 32, 71, 12, 45, 26, 80, 53, 33 };//调用第一种语法格式对 32、71、12、45 进行排序std::stable_sort(myvector.begin(), myvector.begin() 4); //(12 32 45 71) 26 80 53 33//调用第二种语法格式利用STL标准库提供的其它比较规则比如 greaterT进行排序std::stable_sort(myvector.begin(), myvector.begin() 4, std::greaterint()); //(71 45 32 12) 26 80 53 33//调用第二种语法格式通过自定义比较规则进行排序,这里也可以换成 mycomp2()std::stable_sort(myvector.begin(), myvector.end(), mycomp);//12 26 32 33 45 53 71 80//输出 myvector 容器中的元素for (std::vectorint::iterator it myvector.begin(); it ! myvector.end(); it) {std::cout *it ;}return 0;
}stable_sort()函数的使用例子
#include iostream // std::cout
#include algorithm // std::stable_sort
#include vector // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {return (i j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
public:bool operator() (int i, int j) {return (i j);}
};int main() {std::vectorint myvector{ 32, 71, 12, 45, 26, 80, 53, 33 };//调用第一种语法格式对 32、71、12、45 进行排序std::stable_sort(myvector.begin(), myvector.begin() 4); //(12 32 45 71) 26 80 53 33//调用第二种语法格式利用STL标准库提供的其它比较规则比如 greaterT进行排序std::stable_sort(myvector.begin(), myvector.begin() 4, std::greaterint()); //(71 45 32 12) 26 80 53 33//调用第二种语法格式通过自定义比较规则进行排序,这里也可以换成 mycomp2()std::stable_sort(myvector.begin(), myvector.end(), mycomp);//12 26 32 33 45 53 71 80//输出 myvector 容器中的元素for (std::vectorint::iterator it myvector.begin(); it ! myvector.end(); it) {std::cout *it ;}return 0;
}参考链接
C stable_sort()用法详解 C sort()排序函数用法详解
谢谢阅读