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;
}


發佈了57 篇原創文章 · 獲贊 21 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章