64,644
社区成员
发帖
与我相关
我的任务
分享
//给定一个string,在vector<int>中找到和string长度一样的值
#include <iostream>
using std::cout; using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
using std::find_if;
#include <functional>
using std::bind; using std::placeholders::_1;
bool check_size(const string & s, string::size_type sz) {
return s.size() <= sz;
}
void biggies(vector<int> ivec, const string& s) {
auto position = find_if(ivec.cbegin(), ivec.cend(), bind(check_size, s, _1));
cout << *position << endl;
}
int main() {
vector<int> ivec{ 1,2,3,4,5,6,7,8,9,10 };
string sval = "python";
biggies(ivec, sval);
return 0;
}
//从给定的vector<string>里找出长度>=5的并打印出来
#include <iostream>
using std::cout; using std::endl; using std::ends;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
using std::find_if;
#include <functional>
using std::bind; using std::placeholders::_1;
inline void output_words(vector<string> & words) {
for (auto iter = words.cbegin(); iter != words.cend(); ++iter)
cout << *iter << ends;
cout << endl;
}
inline
string make_plural(size_t ctr, const string& word,
const string& ending) {
return (ctr > 1) ? word + ending : word;
}
void elimDups(vector<string>& words) {
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool check_size(const string& s, string::size_type sz) {
return s.size() >= sz;
}
void biggies(vector<string>& words, vector<string>::size_type sz) {
elimDups(words);
for_each(words.begin(), words.end(), [](const string& s) { cout << s << " "; });
cout << endl;
auto wc = partition(words.begin(), words.end(), bind(check_size, _1, sz));
auto count = wc - words.begin();
cout << count << " " << make_plural(count, "word", "s") <<
" of length " << sz << " or longer" << endl;
for_each(words.begin(), wc, [](const string& s) { cout << s << " "; });
cout << endl;
}
int main(int argc, char* argv[]) {
vector<string> words{ "hello","work","dirk","clicked","work","clicked",
"list","vector","list","out","generic","object"};
biggies(words, 5);
return 0;
}