📜  C++中的std :: stable_partition

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

stable_partition()算法安排由开始和结束定义的序列,使得由pfn指定的谓词返回true的所有元素都位于该谓词返回false的元素之前。分区是稳定的。这意味着保留了序列的相对顺序。
句法:

template 
BiIter stable_partition(BiIter start, BiIter end, UnPred pfn);

参数:

start:  the range of elements to reorder
end:  the range of elements to reorder
pfn:  User-defined predicate function object that 
defines the condition to be satisfied if an element is to be classified.
A predicate takes single argument and returns true or false.
Return Value: 
Returns an iterator to the beginning of the elements 
for which the predicate is false.

该函数尝试分配一个临时缓冲区。如果分配失败,则选择效率较低的算法。
范例1:

// CPP program to illustrate stable_partition
#include 
#include 
#include 
using namespace std;
int main()
{
    vector v{ 6, 9, 0, 1, 2, 7, 5, 8, 0 };
    stable_partition(v.begin(), v.end(), [](int n) {return n>0; });
    for (int n : v) {
        cout << n << ' ';
    }
    cout << '\n';
}

输出:

6 9 1 2 7 5 8 0 0 

范例2:

// CPP program to illustrate stable_partition
#include 
#include  // std::stable_partition
#include 
  
bool odd(int i) { return (i % 2) == 1; }
  
int main()
{
    std::vector vct;
  
    for (int i = 1; i < 10; ++i)
        vct.push_back(i); // 1 2 3 4 5 6 7 8 9
  
    std::vector::iterator bound;
    bound = std::stable_partition(vct.begin(), vct.end(), odd);
  
    std::cout << "odd numbers:";
    for (std::vector::iterator it = vct.begin(); it != bound; ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
  
    std::cout << "evennumbers:";
    for (std::vector::iterator it = bound; it != vct.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
  
    return 0;
}

输出:

odd numbers: 1 3 5 7 9
even numbers: 2 4 6 8

范例3:

// CPP program to illustrate stable_partition
#include 
#include 
#include 
#include 
#include 
  
template 
struct is_even : public std::unary_function {
    bool operator()(const Arg& arg1) const
    {
        return (arg1 % 2) == 0;
    }
};
  
int main()
{
    typedef std::deque > Deque;
    typedef std::ostream_iterator >
        Iter;
  
    const Deque::value_type a[] = { 1, 2, 3, 4, 5,
                                    6, 7, 8, 9, 10 };
  
    Deque d1(a + 0, a + sizeof a / sizeof *a);
    Deque d2(d1);
  
    std::cout << "Unpartitioned values: \t\t";
    std::copy(d1.begin(), d1.end(), Iter(std::cout, " "));
  
    std::partition(d2.begin(), d2.end(), is_even());
    std::cout << "\nPartitioned values: \t\t";
    std::copy(d2.begin(), d2.end(), Iter(std::cout, " "));
  
    std::stable_partition(d1.begin(), d1.end(),
                          is_even());
    std::cout << "\nStable partitioned values: \t";
    std::copy(d1.begin(), d1.end(), Iter(std::cout, " "));
    std::cout << std::endl;
  
    return 0;
}

输出:

Unpartitioned values:         1 2 3 4 5 6 7 8 9 10 
Partitioned values:         10 2 8 4 6 5 7 3 9 1 
Stable partitioned values:     2 4 6 8 10 1 3 5 7 9 

复杂性:谓词的确切端启动应用程序,如果没有足够的内存,则最多为(end-start)* log(end-start)交换;如果有足够的内存,则为线性交换数。

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