C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm stable_partition()C++ Algorithm stable_partition() function is used to classify the elements in the range [first, last), in such a way that all the elements for which pred returns true precede all those for which it returns false, where the relative order of elements is preserved. Note: - This function is generally implemented using an internal temporary buffer.Syntaxtemplate <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator stable_partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred); Parameterfirst: An bidirectional iterator pointing to the first element in the range to be partitioned. last: An bidirectional iterator pointing to the past last element in the range to be partitioned. pred: A user-defined unary predicate function that defines the condition to be satisfied if an element is to be classified. Return valueThis function returns an iterator to the first element of the range to not satisfy the predicate condition. ComplexityComplexity is linear in the range [first, last) if enough memory is available: Applies pred to each element. Data racesThe object in the range [first, last) are modified. ExceptionsThis function throws an exception if any of element's comparisons, an element swaps or an operation on iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of stable_partition(): #include <iostream> // std::cout #include <algorithm> // std::stable_partition #include <vector> // std::vector using namespace std; bool IsOdd (int i) { return (i%2)==1; } int main () { vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 vector<int>::iterator bound; bound = stable_partition (myvector.begin(), myvector.end(), IsOdd); // print out content: cout << "odd elements:"; for (vector<int>::iterator it=myvector.begin(); it!=bound; ++it) cout << ' ' << *it; cout << '\n'; cout << "even elements:"; for (vector<int>::iterator it=bound; it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; } Output: odd elements: 1 3 5 7 9 even elements: 2 4 6 8 In the above example, elements from 1 to 9 are partitioned into even and odd elements. Example 2Let's see another simple example: #include <vector> #include <algorithm> #include <iostream> bool greater5 ( int value ) { return value >5; } int main( ) { using namespace std; vector <int> v1, v2; vector <int>::iterator Iter1, Iter2, result; int i; for ( i = 0 ; i <= 10 ; i++ ) v1.push_back( i ); int ii; for ( ii = 0 ; ii <= 4 ; ii++ ) v1.push_back( 5 ); random_shuffle(v1.begin( ), v1.end( ) ); cout << "Vector v1 is ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; // Partition the range with predicate greater10 result = stable_partition (v1.begin( ), v1.end( ), greater5 ); cout << "The partitioned set of elements in v1 is:\n ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; cout << "The first element in v1 to fail to satisfy the" << "\n predicate greater5 is: " << *result << "." << endl; return 0; } Output: Vector v1 is ( 4 10 5 5 5 5 5 1 6 9 3 7 8 2 0 5 ). The partitioned set of elements in v1 is: ( 10 6 9 7 8 4 5 5 5 5 5 1 3 2 0 5 ). The first element in v1 to fail to satisfy the predicate greater5 is: 4. Example 3Let's see another simple example to quick sort the elements of vector using partition() function: #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> v{0, 0, 3, 0, 2, 4, 5, 0, 7}; stable_partition(v.begin(), v.end(), [](int n){return n>0;}); for (int n : v) { cout << n << ' '; } cout << '\n'; return 0; } Output: 3 2 4 5 7 0 0 0 0 Example 4Let's see another simple example: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {1, 0, 3, 4, 5, 6, 12, 7, 9}; stable_partition(v.begin(), v.end(), [](int x) { return x % 3 == 0; }); for_each(v.begin(), v.end(), [](int x) { cout << x << endl; }); return 0; } Output: 0 3 6 12 9 1 4 5 7
Next TopicC++ Algorithm
|