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 

 

转载于:https://www.cnblogs.com/wuchanming/p/3917629.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值