Boost 学习之算法篇 none_of 与 none_of_equal

头文件 '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;
}


在解决算法题目“Powers of Two”时,如果需要查找二进制表示中连续1的数量,可以利用C++标准库中的`std::equal_range`函数。这个函数用于在一个已排序范围内查找两个元素,这两个元素使得所有小于第一个元素的值都小于第二个元素,并且两者之间的范围是连续的。 假设我们有一个包含正整数的数组`arr[]`,我们要找出其中所有等于2的幂次方的索引。我们可以这样做: ```cpp #include <vector> #include <algorithm> // 定义一个函数找到powers_of_two的索引范围 std::pair<int, int> findPowerOfTwo(std::vector<int>& arr) { std::vector<int>::iterator it = arr.begin(), end = arr.end(); // 找到第一个2的幂次方 while (it != end && *it != 1) { ++it; } if (it == end) { return {0, 0}; // 如果数组中没有2的幂次方,返回空范围 } // 使用equal_range寻找连续的2的幂次方 auto range = std::equal_range(it, end, *it); // [first, last) return {range.first - arr.begin(), range.second - arr.begin()}; } int main() { std::vector<int> powers(5, 0); for (auto& index : findPowerOfTwo(powers)) { powers[index] = 1; // 标记找到的2的幂次方位置 } // powers 数组现在应该是 [0, 0, 1, 0, 0] 或者其他表示2的幂的位置 // ... } ``` 在这个例子中,`findPowerOfTwo`函数首先找到数组中第一个2的幂次方,然后使用`std::equal_range`查找从该位置开始的所有连续的2的幂。注意,由于`equal_range`返回的是闭区间 `[first, last)`,所以我们需要减去`arr.begin()`得到实际的索引范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值