改重排sort

对字符串序列,进行先按字节长度大小排序,相同长度的按照字典序排序。

对stable_sort传入更改的排序机制。二元谓词


//
//  main.cpp
//  practise
//
//  Created by zjl on 16/5/9.
//  Copyright © 2016年 zjl. All rights reserved.
//

#include <iostream>
#include <vector>
//#include <>
using namespace std;

void elimDups(vector<string> &words){
    //按字典序排序words,以便查找重复单词
    sort(words.begin(), words.end());
    //unique重排输入范围,使得每个单词只出现一次
    //排列在范围的前部,返回指向不重复区域之后一个位置的迭代器
    auto end_unique = unique(words.begin(),words.end());
    words.erase(end_unique, words.end());
}

bool isShorter(const string& s1, const string& s2){
    return s1.size()<s2.size();
}

int main(int argc, const char * argv[]) {
    vector<string>words={"123","244","435","275","437","435","123","14","3977"};
    elimDups(words);
    stable_sort(words.begin(),words.end(),isShorter);
    for(const auto &s:words)
        cout<<s<<" ";
    cout<<endl;
    return 0;
}


你可能感兴趣的:(C++,sort)