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 

 

相关文章:

  • 2021-05-16
  • 2022-02-17
  • 2022-12-23
  • 2021-08-26
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2022-02-08
  • 2022-02-08
  • 2022-01-30
  • 2022-12-23
  • 2021-11-13
相关资源
相似解决方案