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;
}
运行结果显示如下
相关推荐
牛油果子哥q5 分钟前
【C++ STL string 】C++ STL string 终极精讲:底层原理、内存机制、全套API、深浅拷贝、易错坑点与工程实战规范
数据库·c++
KaMeidebaby6 分钟前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习
Cloud_Shy6187 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
天佑木枫33 分钟前
15天Python入门系列 · 序
开发语言·python
手写码匠1 小时前
从零实现 Prompt 工程引擎:结构化提示、自动优化与多轮自省体系
人工智能·深度学习·算法·aigc
无限码力1 小时前
阿里算法岗 0530笔试真题 - 多约束条件下的元素匹配统计
算法·阿里笔试真题·阿里机试真题·阿里算法岗笔试
lqqjuly1 小时前
MLA — 多头潜在注意力深度解析
深度学习·神经网络·算法
宋拾壹2 小时前
同时添加多个类目
android·开发语言·javascript
吴可可1232 小时前
SolidWorks草图转三维DWG技巧
算法
凡人叶枫2 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发