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 equal_range() C++ Algorithm equal_range() function is the version of binary search. This function is used to return the lower bound and upper bound of the sub range that includes all the elements equivalent to val in the range [first, last). Where sub range is defined...
7 min read
C++ Algorithm Function count_if () C++ Algorithm count_if() function has a 'pred' value and returns the count of the elements in the range [first,last) for which the value of pred is true. Syntax template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last,UnaryPredicate pred); Parameter first: It is an input iterator...
1 min read
C++ Algorithm Functions search_n() C++ Algorithm search_n() function searches the container [first,last) for the occurrence of a sequence of count elements, that is every element is searched to check whether it satisfies a given pred. An iterator to first element satifisying the condition is returned, else an...
2 min read
C++ Algorithm is_sorted() C++ Algorithm is_sorted() function returns true if the elements in the range [first, last) are sorted into ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntax default (1) template <class ForwardIterator> ...
4 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
C++ Algorithm partial_sort_copy() C++ Algorithm partial_sort_copy() function is similar to partial_sort() function which 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 middle and last will be...
8 min read
C++ Algorithm fill_n() C++ Algorithm fill_n() function is used to assign a new value to a specified number of elements in a range beginning with a particular element. It means in fill_n(), we specify beginning position, number of elements to be filled and value to be filled. Syntax template <class...
4 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
C++ Algorithm Functions move backward () The function is used for moving of elements in the backward order, it accepts three arguments and then moves the elements belonging to the range [first,last). The moving of elements begins in the reverse order with termination point at 'result'. Syntax template<class BidirectionalIterator1,...
2 min read
C++ Algorithm binary_search() C++ Algorithm binary_search() function is used check whether the element in the range [first, last) is equivalent to val (or a binary predicate) and false otherwise. The range [first, last) must satisfy all of the following conditions: Partitioned with respect to element < val or comp...
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