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 ); }}
举例详解
#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;
}
该算法测试序列中的所有参数,假如测试这些元素发现其都没有某一特性,则返回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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- boost相关小知识(长期顶置更新)
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解