zoukankan      html  css  js  c++  java
  • find_if函数与partition函数的转换

    编写程序,求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。

    使用find_if实现的代码如下:

    #include<algorithm>
    #include<vector>
    #include<iostream>
    #include<string>
    using namespace std;
    void biggies(vector<string> &words,vector<string>::size_type sz)
    {
        sort(words.begin(),words.end());
        auto end_unique=unique(words.begin(),words.end());
        words.erase(end_unique,words.end());
        stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
        auto wc=find_if(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
        auto count=words.end()-wc;
        cout<<count<<endl;
        for_each(wc,words.end(),[](const string &s) {cout<<s<<" ";});
        cout<<endl;
    }
    int main()
    {
        vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
        biggies(words,5);
        return 0;
    }

    使用partition代码的程序:

    #include<algorithm>
    #include<vector>
    #include<iostream>
    #include<string>
    using namespace std;
    void biggies(vector<string> &words,vector<string>::size_type sz)
    {
        sort(words.begin(),words.end());
        auto end_unique=unique(words.begin(),words.end());
        words.erase(end_unique,words.end());
        stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
        auto wc=partition(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
        auto count=wc-words.begin();
        cout<<count<<endl;
        for_each(words.begin(),wc,[](const string &s) {cout<<s<<" ";});
        cout<<endl;
    }
    int main()
    {
        vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
        biggies(words,5);
        return 0;
    }

    运行结果:

    4
    dfdaaaa dfaaaaa aaaaa aaaaaaa 

     当使用stable_partition后程序:

    #include<algorithm>
    #include<vector>
    #include<iostream>
    #include<string>
    using namespace std;
    void biggies(vector<string> &words,vector<string>::size_type sz)
    {
        sort(words.begin(),words.end());
        auto end_unique=unique(words.begin(),words.end());
        words.erase(end_unique,words.end());
        stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
        for_each(words.begin(),words.end(),[](const string &s) {cout<<s<<" ";});
        cout<<endl;
        auto wc=stable_partition(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
        auto count=wc-words.begin();
        cout<<count<<endl;
        for_each(words.begin(),wc,[](const string &s) {cout<<s<<" ";});
        cout<<endl;
    }
    int main()
    {
        vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
        biggies(words,5);
        return 0;
    }

    运行结果:

    aaa fdaa aaaaa aaaaaaa dfaaaaa dfdaaaa 
    4
    aaaaa aaaaaaa dfaaaaa dfdaaaa 

    说明stable_partiton不改变字典顺序,是稳定的操作。

  • 相关阅读:
    并不对劲的CF1236D&E:Alice&Doll&UnfairGame
    并不对劲的CF1194E:Count The Rectangles
    并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City
    并不对劲的初赛蒙猜凑思路
    并不对劲的CF1237D&E:Balanced Playlist and Binary Search Trees
    并不对劲的???
    并不对劲的P5589
    (简单) POJ 1511 Invitation Cards,SPFA。
    (简单) POJ 3159 Candies,Dijkstra+差分约束。
    (简单) POJ 2502 Subway,Dijkstra。
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3917708.html
Copyright © 2011-2022 走看看