C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Stable partition places all elements in the range [first, last) that satisfy
Stable partition places all elements in the range [first, last) that satisfy stable_partition Header <algorithm> template<class ForwardIterator, class BinaryPredicate> ForwardIterator stable_partition(ForwardIterator first, ForwardIterator last, BinaryPredicate pr) stable_partition places all elements in the range [first, last) that satisfy predicate function pr before all elements that do not satisfy it. stable_partition returns an iterator i such that for any iterator j in the range [first, i), pr(*j) == true, and for any iterator k in the range [i, last), pr(*k) == false. With stable_partition the relative positions of elements in both groups. are preserved. Sample #pragma warning(disable : 4786) #include <algorithm> #include <iostream> #include <functional> bool compare(const int& i) { return (i < 5) ; } int main() { int array[10] = { 9, 11, 2, 4, 17, 12, 1, 8, 3, 10} ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "\n\narray before partition = " ; std::copy(array, array+10, intOstreamIt) ; std::cout << std::endl ; //stable_partition std::stable_partition(array, array+10, compare) ; std::cout << "\n\narray after partition = " ; std::copy(array, array+10, intOstreamIt) ; std::cout << std::endl ; return 0 ; } Program Output array before partition = 9, 11, 2, 4, 17, 12, 1, 8, 3, 10, array after partition = 2, 4, 1, 3, 9, 11, 17, 12, 8, 10,
Privacy Policy
|
Link to Us
|
Links