您的位置:首页 > 编程语言 > C语言/C++

Boost 学习之算法篇 none_of 与 none_of_equal

2016-01-26 09:57 369 查看
头文件 'boost/algorithm/cxx11/none_of.hpp' 包含4个名为none_of的常用算法.

该算法测试序列中的所有参数,假如测试这些元素发现其都没有某一特性,则返回true.

常用的none_of 函数带有一个参数序列以及一个候选值。假如用候选值与参数序列中所有元素比较都返回false,则该函数将返回true。

常用的none_of_equal 函数带一个参数序列和一个值。假如参数序列中的所有元素和传入的值比较都不相同,这返回true。

上述两个函数都有两种调用方式:第一种方式带了一对迭代器,用来标记参数范围;第二种方式方式带了一个用Boost.Range转换后的范围参数。

原文链接:http://www.boost.org/doc/libs/1_60_0/libs/algorithm/doc/html/the_boost_algorithm_library/CXX11/none_of.html

官方API

namespace boost { namespace algorithm { template<typename InputIterator, typename Predicate> bool none_of ( InputIterator first, InputIterator last, Predicate p ); template<typename Range, typename Predicate> bool none_of ( const Range &r, Predicate p ); }}
namespace boost { namespace algorithm {
template<typename InputIterator, typename Predicate>
bool none_of ( InputIterator first, InputIterator last, Predicate p );
template<typename Range, typename Predicate>
bool none_of ( const Range &r, Predicate p );
}}


举例详解
#include <boost/algorithm/cxx11/none_of.hpp>
#include <iostream>
#include <vector>
using namespace boost::algorithm;

bool isOdd(int i){ return i % 2 == 1;}
bool lessThan10(int i){ return i < 10;}

int main()
{
std::vector<int > c;
c.push_back(0);
c.push_back(1);
c.push_back(2);
c.push_back(3);
c.push_back(14);
c.push_back(15);

//false 显然并非所有的元素都为偶数,因此返回false
std::cout<<none_of(c,isOdd) <<std::endl;

//false
std::cout<<none_of(c.begin(),c.end(),lessThan10)<<std::endl;

//true 最后两个数显然与10不相同,返回true
std::cout<<none_of(c.begin()+4,c.end(),lessThan10)<<std::endl;

//true
std::cout<<none_of(c.end(),c.end(),isOdd)<<std::endl;

//false
std::cout<<none_of_equal(c,3)<<std::endl;

//true 前三个元素与3不相同,返回true
std::cout<<none_of_equal(c.begin(),c.begin()+3,3)<<std::endl;
//true
std::cout<<none_of_equal(c.begin(),c.begin(),99)<<std::endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ Boost