sort函数的例子

10.11编写程序,使用stable_sort和isShorter将传递给你的elimDups版本的vector排序。打印vector的内容。

#include<algorithm>

#include<vector>

#include<iostream>

#include<string>

using namespace std;



void elimDup(vector<string> &words)

{
  //按字典序排列 sort(words.begin(),words.end());
  //消除重复的字符串 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() { vector<string> v2={"a","a","dfd","fd","a","df"}; elimDup(v2); for(auto vv:v2) cout<<vv<<" "; cout<<endl;
  //按长度排序之后,保持字典序 stable_sort(v2.begin(),v2.end(),isShorter);
for(auto vv:v2) cout<<vv<<" "; cout<<endl; return 0; }

运行结果如下:

a df dfd fd 

a df fd dfd 

 

你可能感兴趣的:(sort)