C++ Boost庫中的one_of()函數位於標頭“ boost /algorithm /cxx11 /one_of.hpp”下,該標頭測試序列中的元素,如果恰好一個元素共享給定的屬性,則返回true。它接受一個序列和一個謂詞,如果該謂詞恰好對序列中的一個給定元素返回true,則返回true。
用法:
bool one_of ( InputIterator first, InputIterator last, Predicate p )
or
bool one_of ( const Range &R, Predicate p)
參數:該函數接受如下所述的參數:
- first:它指定輸入迭代器到序列中的初始位置。
- second:它指定輸入迭代器到序列中的最終位置。
- p:它指定一個接受元素並返回布爾值的一元謂詞函數。
- R:這是完整的序列。
返回值:如果給定謂詞恰好在序列元素之一上,則該函數返回true,否則返回false。
下麵是上述方法的實現:
程序1::
// C++ program to implement the
// above mentioned function
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/one_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
int c[] = { 1, 2, 3 };
// Run the function with second syntax
bool ans
= boost::algorithm::one_of(c, isOdd);
// Condition to check
if (ans == 1)
cout << "Exactly one element is odd";
else
cout << "Exactly one element is not odd";
return 0;
}
輸出:
Exactly one element is not odd
程序2::
// C++ program to implement the
// above mentioned function
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/one_of.hpp>
using namespace std;
// using boost::algorithm;
// Predicate function to check if
// the elements are less than 7 or not
bool oneLessThanSeven(int i)
{
return i < 7;
}
// Drivers code
int main()
{
// Declares the sequence
int a[] = { 1, 9, 10, 8 };
// Run the function with first syntax
bool ans
= boost::algorithm::one_of(a, a + 4,
oneLessThanSeven);
// Condition to check
if (ans == 1)
cout << "Exactly one element is less than 7";
else
cout << "Exactly one element is not less than 7";
return 0;
}
輸出:
Exactly one element is less than 7
相關用法
- C++ boost::split用法及代碼示例
- C++ boost::algorithm::none_of()用法及代碼示例
- C++ boost::algorithm::is_partitioned()用法及代碼示例
- C++ boost::algorithm::clamp()用法及代碼示例
- C++ boost::algorithm::equal()用法及代碼示例
- C++ boost::algorithm::any_of()用法及代碼示例
- C++ boost::algorithm::none_of_equal()用法及代碼示例
- C++ boost::algorithm::one_of_equal()用法及代碼示例
- C++ boost::algorithm::all_of()用法及代碼示例
- C++ boost::algorithm::all_of_equal()用法及代碼示例
- C++ boost::algorithm::is_sorted()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 boost::algorithm::one_of() in C++ library。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。