当前位置: 首页 > news >正文

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' && 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);stable_sort(words.begin(), words.end(), [](const string &a, const string &b){ return a.size() < b.size(); });auto pos = find_if(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;
}
运行结果显示如下

在这里插入图片描述

http://www.lryc.cn/news/310177.html

相关文章:

  • 【misc | CTF】BUUCTF 二维码
  • OSCP靶场--Resourced
  • Vue路由(黑马程序员)
  • 【Java程序员面试专栏 算法思维】一 高频面试算法题:排序算法
  • sql注入之sqli-labs-less-1 错误注入
  • React withRouter的使用及源码实现
  • c++之旅——第四弹
  • Mysql整理-主从复制
  • 100个百万阅读公众号爆文案例
  • UnityAPI的学习——Transform类
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • GO基本类型
  • 怎么快速编辑视频
  • 基于vue-office实现docx、xlsx、pdf文件的在线预览
  • 数据库系统架构与DBMS功能探微:现代信息时代数据管理的关键
  • 51单片机-(中断系统)
  • c++之旅——第三弹
  • GO—函数
  • [WebUI Forge]ForgeUI的安装与使用 | 相比较于Auto1111 webui 6G显存速度提升60-75%
  • 高刷显示器 - HKC VG253KM
  • javascript实现的星座查询
  • 全国青少年软件编程(Python)等级考试试卷(一级) 测试卷2021年12月
  • 昇腾ACL应用开发之硬件编解码dvpp
  • MFC 模态对话框退出机制的探究
  • Android13 framework添加关机接口
  • 如何使用ArcGIS Pro为栅格图添加坐标信息
  • FDM打印机学习
  • C++进阶-- map和set
  • AI-数学-高中-33概率-事件的关系与运算
  • 数据结构:链队