std::sort()
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
#include <iostream>
#include <vector> //std::vector
#include <algorithm> //std::sort
#include <random> //rand()
#include <time.h> //time()
int compare_greater(int a, int b)
{
return a > b;
}
int compare_less(int a, int b)
{
return a < b;
}
int main(int argc, char** argv)
{
std::vector<int> vec;
srand((unsigned int)time(0));
for(int i = 0; i < 10; i++)
{
vec.push_back(rand());
}
//默认为升序
std::sort(vec.begin(),vec.end());
std::cout << "std::sort(vec.begin(),vec.end()):" << std::endl;
for(auto &iter : vec)
{
std::cout << iter << " ";
}
std::cout <<std::endl;
//手动设置升序
std::sort(vec.begin(),vec.end(),std::less<int>());
std::cout << "std::sort(vec.begin(),vec.end(),std::less<int>()):" << std::endl;
for(auto &iter : vec)
{
std::cout << iter << " ";
}
std::cout << std::endl;
//手动设置降序
std::sort(vec.begin(),vec.end(),std::greater<int>());
std::cout << "std::sort(vec.begin(),vec.end(),std::greater<int>()):" << std::endl;
for(auto &iter : vec)
{
std::cout << iter << " ";
}
std::cout << std::endl;
//自己写排序函数 (升序) //第三个参数可以放一个自己定义的排序规则(这里我用lamda直接写了)
std::sort(vec.begin(),vec.end(),[&](int a, int b){
return a<b;
});
std::sort(vec.begin(), vec.end(),compare_less); //如果看不懂可以这样写compare_less函数见开头
std::cout << "std::sort(vec.begin(),vec.end(),[&](int a, int b){return a<b;}):" << std::endl;
for(auto &iter : vec)
{
std::cout << iter << " ";
}
std::cout << std::endl;
//自己写排序函数 (降序)
std::sort(vec.begin(),vec.end(),[&](int a, int b){
return a>b;
});
//std::sort(vec.begin(), vec.end(),compare_greater); //个人比较喜欢用lamda
std::cout << "std::sort(vec.begin(),vec.end(),[&](int a, int b){return a>b;}):" << std::endl;
for(auto &iter : vec)
{
std::cout << iter << " ";
}
std::cout << std::endl;
return 0;
}
//下面是std::vector<char>。与std::vector<int> 类似
std::vector<char> vec_c; vec_c.push_back('a'); vec_c.push_back('s'); vec_c.push_back('d'); vec_c.push_back('f'); vec_c.push_back('g'); vec_c.push_back('h'); vec_c.push_back('j'); vec_c.push_back('k'); vec_c.push_back('l'); //默认升序 std::sort(vec_c.begin(),vec_c.end());
//同上
//std::sort(vec_c.begin(),vec_c.end(),std::less<char> ());
for(auto &iter : vec_c)
{
std::cout << iter << " ";
}
//输出
std::sort(vec_c.begin(),vec_c.end(),std::greater<char> ());
//同std::vector<int> 一样用在这里就不多说
std::vector<std::string> vec_str; vec_str.push_back("qwe"); vec_str.push_back("asd"); vec_str.push_back("zxc"); vec_str.push_back("uoi"); vec_str.push_back("jkl"); vec_str.push_back("nmb"); vec_str.push_back("bvc"); vec_str.push_back("hgf"); vec_str.push_back("ytr"); std::sort(vec_str.begin(),vec_str.end(),std::greater<std::string> ()); for(auto &iter : vec_str) { std::cout << iter << " "; }
//输出zxc ytr uoi qwe nmb jkl hgf bvc asd
//一样的用法
vec_str.push_back("asd"); vec_str.push_back("aad"); vec_str.push_back("acd"); vec_str.push_back("abd"); vec_str.push_back("aqw"); vec_str.push_back("aer"); vec_str.push_back("afg"); std::sort(vec_str.begin(),vec_str.end(),std::less<std::string> ());
//输出aad abd acd aer afg aqw asd
//还可以这样
std::vector<std::string> vec_str; vec_str.push_back("asd"); vec_str.push_back("aqweqad"); vec_str.push_back("acdadsfwweqwqwe"); vec_str.push_back("abdasdf"); vec_str.push_back("aqwadssf"); vec_str.push_back("werwercxv"); vec_str.push_back("afg"); std::sort(vec_str.begin(),vec_str.end(),[&](std::string str1,std::string str2){ if(str1.length() > str2.length()) { return true; } });
//输出acdadsfwweqwqwe werwercxv aqwadssf aqweqad abdasdf asd afg
怎么定义你的排序规则,只要你高兴都行;不过返回值要是bool类型的;
/
std::sort()对数组排序
int a[] = {1,4,3,2,6,5,8,10,9,15}; std::sort(a,a+9); for(int i = 0; i < 10; i++) { std::cout << a[i] << " "; }
//输出1 2 3 4 5 6 8 9 10 15
//同样的用法不说了 多用几次会了