當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ boost::algorithm::none_of()用法及代碼示例


C++ Boost庫中的none_of()函數位於標頭“ boost /algorithm /cxx11 /none_of.hpp”下,該標頭測試序列中的所有元素,如果所有元素都不共享屬性,則返回true。它接受一個序列和一個謂詞,並且當該謂詞應用於序列中的每個元素時,如果謂詞返回false,則返回true。

用法

bool none_of ( InputIterator first, InputIterator last, Predicate p )
or
bool none_of ( const Range &R, Predicate p)



參數:該函數接受如下所述的參數:

  • first:它指定輸入迭代器到序列中的初始位置。
  • second:它指定輸入迭代器到序列中的最終位置。
  • p:它指定一個接受元素並返回布爾值的一元謂詞函數。
  • R:這是完整的序列。

返回值:如果給定謂詞對於序列中的所有元素均為false,則該函數返回true,否則返回false。

下麵是上述方法的實現:

程序1:

// C++ program to implement the 
// above mentioned function 
  
#include <bits/stdc++.h> 
#include <boost/algorithm/cxx11/none_of.hpp> 
using namespace std; 
// using boost::algorithm; 
  
// Predicate function to check if 
// the element is odd or not 
bool isOdd(int i) 
{ 
    return i % 2 == 1; 
} 
  
// Drivers code 
int main() 
{ 
  
    // Declares the sequence with 
    // 5 length and none elements as 1 
    // [1, 1, 1, 1, 1] 
    vector<int> c(5, 1); 
  
    // Run the function with second syntax 
    bool ans 
        = boost::algorithm::none_of(c, isOdd); 
  
    // Condition to check 
    if (ans == 1) 
        cout << "all are not odd"; 
    else
        cout << "all are odd"; 
    return 0; 
}
輸出:
all are odd

程序2:

// C++ program to implement the 
// above mentioned function 
  
#include <bits/stdc++.h> 
#include <boost/algorithm/cxx11/none_of.hpp> 
using namespace std; 
// using boost::algorithm; 
  
// Predicate function to check if 
// the elements are less than 7 or not 
bool noneLessThanSeven(int i) 
{ 
    return i < 7; 
} 
  
// Drivers code 
int main() 
{ 
  
    // Declares the sequence 
    int a[] = { 1, 2, 5, 6 }; 
  
    // Run the function with first syntax 
    bool ans 
        = boost::algorithm::none_of(a, a + 4, 
                                    noneLessThanSeven); 
  
    // Condition to check 
    if (ans == 1) 
        cout << "all greater than 7"; 
    else
        cout << "all smaller than 7"; 
    return 0; 
}
輸出:
all smaller than 7

參考:https://www.boost.org/doc/libs/1_70_0/libs/algorithm/doc/html/the_boost_algorithm_library/CXX11/none_of.html




相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 boost::algorithm::none_of() in C++ library。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。