C++ algorithm partition

#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>

// g++ 4.9.2 -std=c++11
// function that take a string and return true
// indicating the string has five characters 
// or more
bool pred(const std::string &s)
{
        return !(s.size() < 5);
}

int main()
{
        std::vector<std::string> words;
        std::string str;
        while (std::cin >> str) {
                words.push_back(str);
        }
        std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << "\n";

        auto it = std::partition(words.begin(), words.end(), pred);
        std::copy(words.begin(), it, std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << "\n";

        return 0;
}

from C++ primer 5th Exercise 10.13(P.387)

阅读更多
版权声明:本文为博主原创文章 https://blog.csdn.net/love1937/article/details/49936069
文章标签: C++ 算法
个人分类: C++-Primer
上一篇C++ back_inserter
下一篇C++ lambda algorithm
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭