📜  C++中的std :: is_partitioned

📅  最后修改于: 2021-05-30 12:39:00             🧑  作者: Mango

std :: is_partitioned用于查找range [first,last)是否已分区。如果条件评估为“真”的所有元素都在条件为“假”的那些元素之前,则范围被认为是相对于条件进行分区的。

它在头文件中定义。如果我们要检查分区范围是否为空,则此函数返回true。
句法:

bool is_partitioned (InputIterator first, 
                      InputIterator last, UnaryPredicate pred);

first: Input iterator to the first element in the range.
last: Input iterator to the last element in the range.
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 belongs to
the first group (if true, the element is expected before all
the elements for which it returns false).
The function shall not modify its argument.
This can either be a function pointer or a function object.


Returns: It returns true if all the elements in the range [first, last)
for which pred returns true precede those for which it returns false.
Otherwise it returns false.
If the range is empty, the function returns true.

// C++ program to demonstrate the use of std::is_partitioned
#include 
#include 
#include 
  
// Defining the BinaryFunction
bool pred(int a)
{
    return (a % 3 == 0);
}
  
using namespace std;
int main()
{
    // Declaring first vector
    vector v1 = { 3, 6, 9, 10, 11, 13 };
  
    // Using std::is_partitioned
    bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
  
    if (b == 1) {
        cout << "It is partitioned";
    } else {
        cout << "It is not partitioned.";
    }
    return 0;
}

输出:

It is partitioned

说明:在这里,在此程序中,我们首先将元素存储在向量中,然后检查是否所有被3整除的元素在未被3整除的元素之前都存在。向量,因此此函数在分区时在此处返回1。

另一个例子

  • 检查是否对所有奇数和偶数元素进行了分区
    // C++ program to demonstrate the use of std::is_partitioned
    #include 
    #include 
    #include 
      
    // Defining the BinaryFunction
    bool pred(int a)
    {
        return (a % 2 == 0);
    }
      
    using namespace std;
    int main()
    {
        // Declaring first vector
        vector v1 = { 2, 4, 6, 3, 5, 7, 9 };
      
        // Using std::is_partitioned
        bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "All the even no. are present before odd no.";
        } else {
            cout << "All the even no. are not present before odd no.";
        }
      
        // Inserting an even no. at the end of v1
        // so std::is_partitioned returns false
        v1.push_back(16);
      
        // Now again using std::is_partitioned
        b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "\nAll the even no. are present before odd no.";
        } else {
            cout << "\nAll the even no. are not present before odd no.";
        }
      
        return 0;
    }
    

    输出:

    All the even no. are present before odd no.
    All the even no. are not present before odd no.
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”