C++ Algorithm stable_partition()30 Aug 2024 | 4 min read 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.SyntaxParameterfirst: 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(): 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: 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: Output: 3 2 4 5 7 0 0 0 0 Example 4Let's see another simple example: Output: 0 3 6 12 9 1 4 5 7 Next TopicC++ Algorithm |
C++ Algorithm Function find_if() C++ Algorithm find_if() function returns the value of the first element in the range for which the pred value is true otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies...
2 min read
C++ Algorithm Functions is_permutation() C++ Algorithm is_permutation() function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching even if in different order. The first range is from [first1, last1) and the...
1 min read
C++ Algorithm Function for_each() C++ Algorithm for_each() function applies the function func to all of the elements in the range from 'first' to 'last'. Syntax template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function func); Parameter first: It specifies the first element in the list. last: It specifies the...
4 min read
Introduction: In the dynamic realm of programming, the ability to harness the full potential of hardware resources is a key aspect. Multithreading, the simultaneous execution of multiple threads, plays a crucial role in achieving parallelism and enhancing performance. C++, being a versatile programming language, offers the...
7 min read
C++ Algorithm fill() C++ Algorithm fill() function is used to assign the same new value to every element in a specified range[first, end) by using operator=. Note: Range [first, last) means first is included in the range but last is not included. Syntax template <class ForwardIterator, class T> void...
4 min read
C++ Algorithm partial_sort() C++ Algorithm partial_sort() function is used to rearrange the elements in the range[first, last), in such a way that the elements between the first and middle will be sorted and the elements between the middle and last will be in an unspecified order. The...
6 min read
C++ Algorithm Function count() C++ Algorithm count()function accepts 'val' as argument and compares for the occurrence of element 'val' in the range. The number of occurrence of that element is returned. Syntax template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); Parameter first: It is...
1 min read
C++ Algorithm nth_element() C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and...
6 min read
C++ Algorithm Function any_of() C++ Algorithm any_of() function tests the value of 'pred' for every element in the range, if for any element the value of pred is true, then the function returns true else return false. Syntax template <class InputIteratir, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate...
1 min read
C++ Algorithm generate_n() C++ Algorithm generate_n() function is used to assign the values which is generated by a function object to a specified number of elements in a range and returns to the one past the last assigned value position. The generator function is defined by the...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India