当前位置:   article > 正文

C++ //练习 10.16 使用lambda编写你自己版本的biggies。

C++ //练习 10.16 使用lambda编写你自己版本的biggies。

C++ Primer(第5版) 练习 10.16

练习 10.16 使用lambda编写你自己版本的biggies。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
	> File Name: ex10.16.cpp
	> Author: 
	> Mail: 
	> Created Time: Fri 01 Mar 2024 02:49:29 PM CST
 ************************************************************************/

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

string make_plural(size_t ctr, string &word, const string &ending = "s"){
    int size = word.size();
    if(ctr <= 1){
        return word;
    }
    else{
        if(word[size-1] == 's' || word[size-1] == 'x' || (word[size-1] == 'h' && 
                                  word[size-2] == 's') || (word[size-1] == 'h' && wo
            return word + "e" + ending;
        }
        else if(word[size-1] == 'y' && (word[size-2] != 'a' && word[size-2] != 'e' &
                                        word[size-2] != 'i' && word[size-2] != 'o' &
            word[size-1] = 'i';
            return word + "e" + ending;
        }
        else if((word[size-3] != 'a' && word[size-3] != 'e' && word[size-3] != 'i' &
                 word[size-3] != 'o' && word[size-3] != 'u') && (word[size-2] != 'a'
                 word[size-2] != 'e' && word[size-2] != 'i' && word[size-2] != 'o' &
            if(word[size-1] == 'f'){
                word[size-1] = 'v';
                return word + ending;
            }
            else if(word[size-2] == 'f' && word[size-1] == 'e'){
                word[size-2] = 'v';
                return word + "e" + ending;
            }
        }
        else{
            return word + ending;
        }
    }
    return word;
}

void elimDups(vector<string> &words){
    sort(words.begin(), words.end());
    auto end = unique(words.begin(), words.end());
    words.erase(end, words.end());
}

void biggies(vector<string> &words, vector<string>::size_type len){
    elimDups(words);
    stable_sort(words.begin(), words.end(), [](const string &a, const string &b){ re

    auto pos = find_if(words.begin(), words.end(), [len](const string &a){ return a.

    auto count = words.end() - pos;
    string str("word");
    cout<<count<<" "<<make_plural(count, str, "s")<<" of length "<<len<<" or longer"

    for_each(pos, words.end(), [](const string &s){ cout<<s<<" "; });

    cout<<endl;
}

int main(){
    vector<string> words;
    string str;

    cout<<"Enter strings: ";
    while(cin>>str){
        words.push_back(str);
        if(cin.get() == '\n'){
            break;
        }
    }

    biggies(words, 5);

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
运行结果显示如下

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/181364
推荐阅读
相关标签
  

闽ICP备14008679号