C++ Algorithm partition()30 Aug 2024 | 4 min read C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. 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): Applies pred to each element, and possibly swaps some of them. Data racesThe object in the range [first, last) are modified. ExceptionsThis function throws an exception if either an element swap 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 partition(): Output: odd elements: 1 9 3 7 5 even elements: 6 4 8 2 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 7 8 0 5 2 1 6 9 3 ). The partitioned set of elements in v1 is: ( 9 10 7 8 6 5 2 1 0 4 3 ). Example 3Let's see another simple example to quick sort the elements of vector using partition() function: Output: Original vector: 0 1 2 3 4 5 6 7 8 9 Partitioned vector: 0 8 2 6 4 * 5 3 7 1 9 Unsorted list: 1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92 Sorted using quicksort: -8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92 Example 4Let's see another simple example: Output: Before Partition: 1 2 3 4 5 After partition: 4 2 3 1 5 Is it partitioned? Yes, it is partitioned Next TopicC++ Algorithm |
C++ Algorithm stable_sort() C++ Algorithm stable_sort() function is used to sort the elements in the range [first, last) into ascending order like sort but keeps the order of equivalent elements. The elements are compared using operator < for the first version, and comp for the second version. Syntax template <class...
6 min read
C++ Algorithm swap_ranges() C++ Algorithm swap_ranges() exchanges the elements in the range [first1, last2) with the elements present in the range starting from first2. In short we can say that, swap_ranges() swaps the elements of two sequence, that is each element at a position in the first sequence...
5 min read
C++ Algorithm is_sorted_until() C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for...
4 min read
C++ Algorithm iter_swap() C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. Syntax template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); Parameter a: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is...
7 min read
C++ Algorithm Function copy() C++ Algorithm copy() function is used to copy all the elements of the container [first,last] into a different container starting from result. Syntax template<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is an input iterator to the first element of the range,...
1 min read
C++ Algorithm replace() C++ Algorithm replace() function is used to replace all value equal to old_value by the value new_value in the range [first, last). This function examines each element in the range and replaces it if it matches a specified value. Syntax template <class ForwardIterator, class T> void...
4 min read
C++ Algorithm upper_bound() C++ Algorithm upper_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in the range [first, last) that is greater than to the specified value val. The first version uses operator < to compare...
5 min read
C++ Algorithm Functions none_of() C++ Algorithm none_of() function returns a true value if the value of 'pred' argument is false. The value should be false for all elements in the range [first, last). Syntax template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first : It specifies...
2 min read
C++ Algorithm Function equal() C++ Algorithm equal()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. The first range is from [first1,last1) and the second starts from first2. Syntax template<class InputIterator1, class...
2 min read
C++ Algorithm remove() C++ Algorithm remove() function is used to eliminate all the elements that are equal to val from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the...
5 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