zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    产品微谈
    SVN回滚机制
    super究竟是个啥?
    PM12条
    CocoaPods初体验
    UIView局部点击
    Memory cycles about Block
    About "self"
    openfire学习(一)
    WPF菜单和布局(2)
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3917629.html
Copyright © 2011-2022 走看看