1. 程式人生 > >c++ std::sort用法詳細(對std::vector, 對std::vector, std::vector等等)

c++ std::sort用法詳細(對std::vector, 對std::vector, std::vector等等)

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
//同樣的用法不說了 多用幾次會了