該算法測試序列中的所有參數,假如測試這些元素髮現其都沒有某一特性,則返回true.
常用的none_of 函數帶有一個參數序列以及一個候選值。假如用候選值與參數序列中所有元素比較都返回false,則該函數將返回true。
常用的none_of_equal 函數帶一個參數序列和一個值。假如參數序列中的所有元素和傳入的值比較都不相同,這返回true。
上述兩個函數都有兩種調用方式:第一種方式帶了一對迭代器,用來標記參數範圍;第二種方式方式帶了一個用Boost.Range轉換後的範圍參數。
官方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;
}