删除容器中重复字符串并按长度排序…
来源:互联网 发布:我知谁掌管明天歌词 编辑:程序博客网 时间:2024/05/23 19:19
#include < iostream> >
#include < algorithm> >
#include < string> >
#include < vector> >
using namespace std;
string make_plural(size_t ctr, const string &word,
const string&ending)
{
return (ctr> 1) ? word + ending : word;
}
bool IsShorter(const string &str1, const string&str2)
{
returnstr1.size() < str2.size();
}
void ElimDups(vector &words)
{
sort(words.begin(), words.end());
autoend_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
void BiggiesWithFind_if(vector &words,
vector::size_type sz)
{
//按字典排序,删除重复单词
ElimDups(words);
//按长度排序,长度相同的维持字典序
stable_sort(words.begin(), words.end(),
[](const string &str1, const string&str2)
{returnstr1.size() < str2.size(); });
//获取一个迭代器,指向第一个满足size() > sz的元素
auto wc =find_if(words.begin(), words.end(),
[=](const string &s)//可以使用隐式捕获,编译器会自己推断捕获内容
//捕获引用使用'&' , 捕获值使用'='
{returns.size() >= sz; });
//计算满足条件元素的数目
auto count =words.end() - wc;
cout<< count << " " << make_plural(count, "word","s")
<< " oflength " << sz << " or longer" << endl;
//打印每个长度大于等于要求的值的单词, 每个单词后面接一个空格
for_each(wc,words.end(),
[](const string &s) {cout << s <<" "; });
cout<< endl;
}
void BiggiesWithPartition(vector &words,
vector::size_type sz)
{
ElimDups(words);
auto wc =partition(words.begin(), words.end(),
[sz](const string &str) {return str.size()< sz; });
//计算满足条件元素的数目
auto count =words.end() - wc;
cout<< count << " " << make_plural(count, "word","s")
<< " oflength " << sz << " or longer" << endl;
//打印每个长度大于等于要求的值的单词, 每个单词后面接一个空格
for_each(wc,words.end(),
[](const string &s) {cout << s <<" "; });
cout<< endl;
}
int main(int argc, char **argv)
{
vectorstr_vec{ "the", "red", "fox", "jump","over", "the", "slow", "red", "turtle" };
BiggiesWithFind_if(str_vec, 4);
cout<< "-------------------" << endl;
BiggiesWithPartition(str_vec, 4);
return0;
}
#include < algorithm> >
#include < string> >
#include < vector> >
using namespace std;
string make_plural(size_t ctr, const string &word,
{
}
bool IsShorter(const string &str1, const string&str2)
{
}
void ElimDups(vector &words)
{
}
void BiggiesWithFind_if(vector &words,
{
}
void BiggiesWithPartition(vector &words,
{
}
int main(int argc, char **argv)
{
}
0 0
- 删除容器中重复字符串并按长度排序…
- 删除容器中重复字符串并按长度排序…
- 去掉字符串中重复的字符并排序
- delphi中按指定字符重复生成指定长度字符串
- 删除字符串中重复字符
- 删除字符串中重复字符
- 删除字符串中重复字符
- 删除字符串中重复字符
- 删除字符串中重复元素
- 删除字符串中重复字符
- 删除字符串中重复字符。
- 尾单词长度、1 3 9 27 81 实现1-121任意算法、去除重复字符并排序、拼音转数字、按要求分解字符串
- Python 字符串,list排序,删除重复数据
- 将6个字符串存入vector容器中,并采用一种STL算法排序并输出
- 删除字符串中重复的字符串
- 【整理】删除顺序容器(如:vector)中的重复字符串
- 删除无序数组中跳跃重复值并排序(时间复杂度为O(n))
- 关于数组的.sort()方法案例:删除数组中重复的元素并排序
- 声明vector对象保存函数指针
- 博客中尖括号不显示的问题
- C# 实现的 AES 加密算法
- 静态成员在类中的初始化
- 输入一串字符, 统计特定字符个数
- 删除容器中重复字符串并按长度排序…
- 捕获一个int变量, 判断是否…
- 找出最大的有由个三位数乘积构成的…
- 即时通讯中语音视频聊天后面的技术支持有哪些
- 简单的读写文件
- ubuntu14安装tomcat6
- 插入排序与快排
- 接受一个&istream参数, …
- 使用流迭代器, sort, co…