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;
}
运行结果显示如下
相关推荐
挥剑决浮云 -11 分钟前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
Mephisto.java25 分钟前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli26 分钟前
滑动窗口->dd爱框框
算法
丶Darling.28 分钟前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo52038 分钟前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
wjs202443 分钟前
XSLT 实例:掌握 XML 转换的艺术
开发语言
萧鼎1 小时前
Python第三方库选择与使用陷阱避免
开发语言·python
安冬的码畜日常1 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js
jiyisuifeng19911 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
小柯J桑_1 小时前
C++:STL简介
c++·stl