c++ primer p349 biggies的实现

要求:

随机输入多个字符串,最后输ctrl+z,回车。输入想要显示的字符串的最小长度。

将字符串按长度和大小排序。然后输出满足条件的字符串。

代码如下:

#include "pch.h"
#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
void elimdups(vector<string>& word);
void biggies(vector<string>& word, vector<string>::size_type sz);
bool judgelength(const string&, const string&);   //这里这个函数需要两个参数,在stable_sort函数内部具体如何实现的,咱也不清楚呢
int main()
{
	vector<string> a;
	string b;
	while (cin >> b) {
		a.push_back(b);
	}
	cout << "请输入你想显示的字符串的最小长度" << endl;
	vector<string>::size_type c;
	cin >> c;
	biggies(a,c);
}
void biggies(vector<string>& word, vector<string>::size_type sz) {
	//1.排序并且删除相同单词
	elimdups(word);
	//2.按长度再次进行排序
	stable_sort(word.begin(),word.end(),judgelength);
	//3.找到满足长度大小的string的首地址
	auto wc = find_if(word.begin(), word.end(), [sz](const string& a) {return a.size() > sz; });
	//4.输出满足条件的所有string
	for_each(wc, word.end(), [](const string& a) {cout << a << " "; });
	cout << endl;
}
void elimdups(vector<string>& word) {
	string b;
	while (cin >> b) {
		word.push_back(b);
	}
	sort(word.begin(), word.end());   //按大小进行排序
	auto end_unique = unique(word.begin(), word.end());  //将重复的单词覆盖掉
	word.erase(end_unique, word.end());          //将word的世界size改为世界大小
}
bool judgelength(const string& a, const string& b) {
	return a.size() < b.size();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值