C++ //练习 10.19 用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。

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

练习 10.19 用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex10.19.cpp
	> Author: 
	> Mail: 
	> Created Time: Sat 02 Mar 2024 08:24: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' && word[size-2] == 'c')){
            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-2] != 'u')){
            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' && word[size-2] != 'u')){
            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);

    auto pos = stable_partition(words.begin(), words.end(), [len](const string &a){ return a.size() < len; });

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

    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;
}
运行结果显示如下
相关推荐
青草地溪水旁2 分钟前
UML函数原型中guard的含义,有啥用?
c++·uml
Pocker_Spades_A22 分钟前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
百度Geek说33 分钟前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven39 分钟前
轻松掌握数据结构:二叉树
后端·算法·面试
big_eleven42 分钟前
轻松掌握数据结构:二叉查找树
后端·算法·面试
CoovallyAIHub1 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·计算机视觉
CHEN5_021 小时前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o2 小时前
C语言基础:变量与进制详解
java·c语言·开发语言
kyle~2 小时前
OpenCV---特征检测算法(ORB,Oriented FAST and Rotated BRIEF)
人工智能·opencv·算法
初学小刘2 小时前
决策树:机器学习中的强大工具
算法·决策树·机器学习