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 分钟前
Easyx图形库应用(用lua开发图形界面)
开发语言·lua
ellis197011 分钟前
Lua协程coroutine库用法
开发语言·lua
nwsuaf_huasir31 分钟前
matlab构造带通巴特沃斯滤波器进行滤波
开发语言·matlab
救救孩子把32 分钟前
从 JDK 8 到 JDK 23:HotSpot 垃圾回收器全景演进与深度剖析
java·开发语言·jvm·jdk
清辞8531 小时前
C++入门(底层知识C与C++的不同)
开发语言·c++·算法
fqbqrr1 小时前
2510C++,api设计原则,不除零
开发语言·c++
~kiss~1 小时前
图像处理~多尺度边缘检测算法
图像处理·算法·计算机视觉
fqbqrr1 小时前
2510d,C++虚混杂
c++·d
Mr.看海1 小时前
机器学习鼻祖级算法——使用SVM实现多分类及Python实现
算法·机器学习·支持向量机
科比不来it1 小时前
Go语言数据竞争Data Race 问题怎么检测?怎么解决?
开发语言·c++·golang