📜  C++中的std :: none_of

📅  最后修改于: 2021-05-30 08:31:59             🧑  作者: Mango

如果pred对范围[first,last]中的所有元素返回false或范围为空,则返回true,否则返回false。
句法 :

Template :
bool none_of(InputIterator first, InputIterator last,
                                 UnaryPredicate pred);
first, last
Input iterators to the initial and final positions in
a sequence. The range used is [first, last], which 
contains all the elements between first and last, 
including the element pointed by first but not the
element pointed by last.

pred
Unary function that accepts an element in the range
as argument and returns a value convertible to bool. 
The value returned indicates whether the element 
fulfills the condition checked by this function.
The function shall not modify its argument.
This can either be a function pointer or a function
object. 

Return type :
true if pred returns false for all the elements in 
the range [first, last] or if the range is empty, 
and false otherwise.
CPP
// CPP program to illustrate std :: none_of
#include  // cout
#include  // none_of
using namespace std;
 
// function to check whether the
// element is negative or not
bool comp(int a) {  return a < 0;  }
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 12, 0 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << "Array contains :";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << "\n";
 
    if (none_of(arr, arr+n, comp))
        cout << "No negative elements in the range.\n";
    else
        cout << "There is at least one negative"
               " element in the array range.\n";
    return 0;
}


CPP
// CPP program to illustrate std :: none_of
#include  // cout
#include  // none_of
using namespace std;
 
// functions to check whether the
// element is even or odd
bool isEven(int a) { return (a % 2); }
bool isOdd(int a)  { return (a % 2 == 0); }
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 12, 0 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << "Array contains :";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << "\n";
 
    bool even = none_of(arr, arr + n, isEven);
    bool odd =  none_of(arr, arr + n, isOdd);
 
    if ((!even) && (!odd))
        cout << "Contains both even and"
               " odd number\n";
    else if ((!even) && odd)
        cout << "Contains odd number only\n";
    else if (even && (!odd))
        cout << "Contains even number only\n";
    else
        cout << "Array is empty\n";
 
    return 0;
}


CPP
// CPP program to illustrate std :: none_of
#include  // none_of
#include  // cout
using namespace std;
 
// Function reference :
// https://www.geeksforgeeks.org/primality-test-set-
// 1-introduction-and-school-method/
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 6, 8, 12, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Array contains :";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << "\n";
 
    if (none_of(arr, arr + n, isPrime))
        cout << "All numbers are composite.\n";
    else
        cout << "There are primes in array \n";
    return 0;
}


输出
Array contains : 2 4 6 8 12 0
No negative elements in the range.

实际应用:
如果[[first,last]]范围内的所有元素的某些条件返回false,或者如果范围为空,则std :: none_of函数将返回true,否则返回false。
1.检查数组是否包含所有偶数或奇数或两者。

CPP

// CPP program to illustrate std :: none_of
#include  // cout
#include  // none_of
using namespace std;
 
// functions to check whether the
// element is even or odd
bool isEven(int a) { return (a % 2); }
bool isOdd(int a)  { return (a % 2 == 0); }
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 12, 0 };
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << "Array contains :";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << "\n";
 
    bool even = none_of(arr, arr + n, isEven);
    bool odd =  none_of(arr, arr + n, isOdd);
 
    if ((!even) && (!odd))
        cout << "Contains both even and"
               " odd number\n";
    else if ((!even) && odd)
        cout << "Contains odd number only\n";
    else if (even && (!odd))
        cout << "Contains even number only\n";
    else
        cout << "Array is empty\n";
 
    return 0;
}
输出
Array contains : 2 4 6 8 12 0
Contains even number only

2.检查数组是否包含所有素数。

CPP

// CPP program to illustrate std :: none_of
#include  // none_of
#include  // cout
using namespace std;
 
// Function reference :
// https://www.geeksforgeeks.org/primality-test-set-
// 1-introduction-and-school-method/
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 6, 8, 12, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Array contains :";
    for (int i = 0; i < n; i++)
        cout << ' ' << arr[i];
    cout << "\n";
 
    if (none_of(arr, arr + n, isPrime))
        cout << "All numbers are composite.\n";
    else
        cout << "There are primes in array \n";
    return 0;
}
输出
Array contains : 4 6 8 12 0
All numbers are composite.
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”