📜  C++ STL中的std :: partition

📅  最后修改于: 2021-05-25 21:57:26             🧑  作者: Mango

C++在其STL算法库中有一个类,该类允许我们使用某些内置函数轻松进行分区算法。分区是指根据给定条件划分容器元素的动作。
分区操作
1. partition(beg,end,condition) :-此函数用于根据其参数中提到的条件对元素进行分区
2. is_partitioned(beg,end,condition) :-如果容器已分区,则此函数返回布尔值true,否则返回false。

CPP
// C++ code to demonstrate the working of
// partition() and is_partitioned()
#include
#include // for partition algorithm
#include // for vector
using namespace std;
int main()
{
    // Initializing vector
    vector vect = { 2, 1, 5, 6, 8, 7 };
     
    // Checking if vector is partitioned
    // using is_partitioned()
    is_partitioned(vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
         
    })?
     
    cout << "Vector is partitioned":
    cout << "Vector is not partitioned";
    cout << endl;
     
    // partitioning vector using partition()
    partition(vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
         
    });
     
    // Checking if vector is partitioned
    // using is_partitioned()
    is_partitioned(vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
         
    })?
     
    cout << "Now, vector is partitioned after partition operation":
    cout << "Vector is still not partitioned after partition operation";
    cout << endl;
     
    // Displaying partitioned Vector
    cout << "The partitioned vector is : ";
    for (int &x : vect) cout << x << " ";
     
    return 0;
     
}


CPP
// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#include
#include // for partition algorithm
#include // for vector
using namespace std;
int main()
{
    // Initializing vector
    vector vect = { 2, 1, 5, 6, 8, 7 };
     
    // partitioning vector using stable_partition()
    // in sorted order
    stable_partition(vect.begin(), vect.end(), [](int x)
    {
        return x%2 == 0;       
    });
     
    // Displaying partitioned Vector
    cout << "The partitioned vector is : ";
    for (int &x : vect) cout << x << " ";
    cout << endl;
     
    // Declaring iterator
    vector::iterator it1;
     
    // using partition_point() to get ending position of partition
    auto it = partition_point(vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
    });
     
    // Displaying partitioned Vector
    cout << "The vector elements returning true for condition are : ";
    for ( it1= vect.begin(); it1!=it; it1++)
    cout << *it1 << " ";
    cout << endl;
     
    return 0;
     
}


CPP
// C++ code to demonstrate the working of
// partition_copy()
#include
#include // for partition algorithm
#include // for vector
using namespace std;
int main()
{
    // Initializing vector
    vector vect = { 2, 1, 5, 6, 8, 7 };
     
    // Declaring vector1
    vector vect1;
     
    // Declaring vector1
    vector vect2;
     
    // Resizing vectors to suitable size using count_if() and resize()
    int n = count_if (vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
         
    } );
    vect1.resize(n);
    vect2.resize(vect.size()-n);
     
    // Using partition_copy() to copy partitions
    partition_copy(vect.begin(), vect.end(), vect1.begin(),
                                     vect2.begin(), [](int x)
    {
        return x%2==0;
    });
     
     
    // Displaying partitioned Vector
    cout << "The elements that return true for condition are : ";
    for (int &x : vect1)
            cout << x << " ";
    cout << endl;
     
    // Displaying partitioned Vector
    cout << "The elements that return false for condition are : ";
    for (int &x : vect2)
            cout << x << " ";
    cout << endl;
     
    return 0;   
}


输出:

Vector is not partitioned
Now, vector is partitioned after partition operation
The partitioned vector is : 2 8 6 5 1 7

在上面的代码中,分区函数根据元素是偶数还是奇数对向量进行分区,偶数元素从奇数元素开始的划分没有特定顺序。
3. stable_partition(beg,end,condition) :-此函数用于根据其自变量中提到的条件对元素进行分区从而保留元素的相对顺序。
4. partition_point(beg,end,condition) :-此函数返回一个迭代器,该迭代器指向容器的分区点,即条件不成立的分区范围[beg,end)中的第一个元素。容器应该已经分区了,此函数才能正常工作。

CPP

// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#include
#include // for partition algorithm
#include // for vector
using namespace std;
int main()
{
    // Initializing vector
    vector vect = { 2, 1, 5, 6, 8, 7 };
     
    // partitioning vector using stable_partition()
    // in sorted order
    stable_partition(vect.begin(), vect.end(), [](int x)
    {
        return x%2 == 0;       
    });
     
    // Displaying partitioned Vector
    cout << "The partitioned vector is : ";
    for (int &x : vect) cout << x << " ";
    cout << endl;
     
    // Declaring iterator
    vector::iterator it1;
     
    // using partition_point() to get ending position of partition
    auto it = partition_point(vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
    });
     
    // Displaying partitioned Vector
    cout << "The vector elements returning true for condition are : ";
    for ( it1= vect.begin(); it1!=it; it1++)
    cout << *it1 << " ";
    cout << endl;
     
    return 0;
     
}

输出:

The partitioned vector is : 2 6 8 1 5 7 
The vector elements returning true for condition are : 2 6 8

在上面的代码中,偶数和奇数元素按升序进行了划分(排序)。但是,并非总是按升序排列,这里的元素(偶数和奇数)按升序出现,因此分区后的结果也是如此。如果vect在stable_partition()之后为{2,1,7,8,6,5},则它将为{2,8,6,1,7,5}。外观顺序保持不变。
5. partition_copy(beg,end,beg1,beg2,condition) :-此函数将其分区中的分区元素复制到其参数中提到的differnet容器中。它需要5个参数。容器的开始和结束位置,必须复制元素的新容器的开始位置(元素返回条件为true),必须复制其他元素的新容器的开始位置(元素返回条件为false的元素)和condition调整新容器的大小对于此函数是必需的。

CPP

// C++ code to demonstrate the working of
// partition_copy()
#include
#include // for partition algorithm
#include // for vector
using namespace std;
int main()
{
    // Initializing vector
    vector vect = { 2, 1, 5, 6, 8, 7 };
     
    // Declaring vector1
    vector vect1;
     
    // Declaring vector1
    vector vect2;
     
    // Resizing vectors to suitable size using count_if() and resize()
    int n = count_if (vect.begin(), vect.end(), [](int x)
    {
        return x%2==0;
         
    } );
    vect1.resize(n);
    vect2.resize(vect.size()-n);
     
    // Using partition_copy() to copy partitions
    partition_copy(vect.begin(), vect.end(), vect1.begin(),
                                     vect2.begin(), [](int x)
    {
        return x%2==0;
    });
     
     
    // Displaying partitioned Vector
    cout << "The elements that return true for condition are : ";
    for (int &x : vect1)
            cout << x << " ";
    cout << endl;
     
    // Displaying partitioned Vector
    cout << "The elements that return false for condition are : ";
    for (int &x : vect2)
            cout << x << " ";
    cout << endl;
     
    return 0;   
}

输出:

The elements that return true for condition are : 2 6 8 
The elements that return false for condition are : 1 5 7
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”