std::stable_partition in C++

The stable_partition( ) algorithm arranges the sequence defined by start and end such that all elements for which the predicate specified by pfn returns true come before those for which the predicate returns false. The partitioning is stable. This means that the relative ordering of the sequence is preserved.
Syntax:

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

Parameters:

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.

This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
Example 1:

// CPP program to illustrate stable_partition
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    vector<int> 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';
}

Output:

6 9 1 2 7 5 8 0 0 

Example 2:



// CPP program to illustrate stable_partition
#include <iostream>
#include <algorithm> // std::stable_partition
#include <vector>

bool odd(int i) { return (i % 2) == 1; }

int main()
{
    std::vector<int> vct;

    for (int i = 1; i < 10; ++i)
        vct.push_back(i); // 1 2 3 4 5 6 7 8 9

    std::vector<int>::iterator bound;
    bound = std::stable_partition(vct.begin(), vct.end(), odd);

    std::cout << "odd numbers:";
    for (std::vector<int>::iterator it = vct.begin(); it != bound; ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    std::cout << "evennumbers:";
    for (std::vector<int>::iterator it = bound; it != vct.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

Output:

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

Example 3:

// CPP program to illustrate stable_partition
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>

template <class Arg>
struct is_even : public std::unary_function<Arg, bool> {
    bool operator()(const Arg& arg1) const
    {
        return (arg1 % 2) == 0;
    }
};

int main()
{
    typedef std::deque<int, std::allocator<int> > Deque;
    typedef std::ostream_iterator<int, char,
                                  std::char_traits<char> >
        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<int>());
    std::cout << "\nPartitioned values: \t\t";
    std::copy(d2.begin(), d2.end(), Iter(std::cout, " "));

    std::stable_partition(d1.begin(), d1.end(),
                          is_even<int>());
    std::cout << "\nStable partitioned values: \t";
    std::copy(d1.begin(), d1.end(), Iter(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

Output:

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 

Complexity: Exactly end-start applications of the predicate and at most (end-start)*log(end-start) swaps if there is insufficient memory or linear number of swaps if sufficient memory is available.

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)






User Actions