C++ Primer 第5版--练习10.13

来源:互联网 发布:开源软件使用许可 编辑:程序博客网 时间:2024/06/06 14:28

练习 10.13:标准库定义了名为 partition 的算法,它接受一个谓词,对容器内容进行划分,使得谓词为 true 的值会排在容器的前半部分,而使谓词为 false 的值会排在后半部分。算法返回一个迭代器,指向最后一个使谓词为 true 的元素之后的位置。编写函数,接受一个 string,返回一个 bool 值,指出 string 是否有5个或更多字符。使用此函数划分 words。打印出长度大于等于5的元素。

#include <iostream>#include <vector>#include <algorithm>using namespace std;bool biggerThan5(const string &s){    return s.size() >= 5;}int main(){    vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};    auto end_bigger = partition(words.begin(), words.end(), biggerThan5);    cout << "words中长度大于等于5的元素为:";    for (auto iter = words.begin(); iter != end_bigger; ++iter)        cout << *iter << " ";    cout << endl;    return 0;}


0 0
原创粉丝点击