要求:
随机输入多个字符串,最后输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();
}