C++ Algorithm partition_copy()30 Aug 2024 | 4 min read C++ Algorithm partition_copy() function is used to copy the elements for which a condition is true to one destination, and false to another. The elements must belong to a specified range. SyntaxParameterfirst: An input iterator pointing to the first element in the range to check for a condition. last: An input iterator pointing to the past last element of the range. pred: A user-defined unary predicate function that defines the condition to be tested. result_true: An output iterator used to copy elements for which pred returns true. result_false: An output iterator used to copy elements for which pred returns false. Return valueThis function returns a pair of iterators with the end of generated sequences addressed by result_true and result_false, respectively. ComplexityComplexity is linear in the range [first, last) if enough memory is available: Applies pred to each element and performs an assignment for each element. Data racesThe object in the range [first, last) are accessed where the each element is accessed exactly once. ExceptionsThis function throws an exception if any of pred, an element's assignment or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of partition_copy(): Output: odd is: 1 3 5 7 9 even is: 2 4 6 8 Example 2Let's see another simple example: Output: true_arr: 6 7 8 9 10 false_arr: 1 2 3 4 5 Example 3Let's see another simple example: Output: v : 1,2,3,4,5, evens : 2,4, odds : 1,3,5, Example 4Let's see another simple example: Output: Evens : 4 Odds : 5 Contents of the vector is : [0] : 2 [1] : 4 [2] : 6 [3] : 8 Contents of the vector is : [0] : 1 [1] : 3 [2] : 5 [3] : 7 [4] : 9 Next TopicC++ Algorithm |
C++ Algorithm shuffle() C++ Algorithm shuffle() function reorders the elements of a range by putting them at random places, using g as uniform random number generator. Syntax template <class RandomAccessIterator, class URNG> void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g); Parameter first: A random access iterator pointing the position of...
2 min read
C++ Algorithm max() C++ Algorithm max() function can be used in following 3 ways: It compares the two values passed in its arguments and returns the larger between them. If both are equal, then it returns the first one. It also compares the two values using a binary...
5 min read
C++ Algorithm partition() 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. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
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:...
4 min read
C++ Algorithm transform() C++ Algorithm transform() function is used in two different ways: 1.unary operation:- This method performs unary operation op on the elements in range [first1, last1] and stores the result in range starting from result. This transform() applies a function to each element of a range: 2.Binary...
5 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 sort() C++ Algorithm sort() function is used to sort the elements in the range [first, last) into ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntax default (1) template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last); custom...
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 generate () C++ Algorithm generate() function is used to assign the value generated by a function object to each element in a range. The generator function is defined by the user and it is called successively for assigning the numbers. Syntax template <class ForwardIterator, class Generator> void...
4 min read
C++ Algorithm unique() C++ Algorithm unique() function is used to transform a sequence in such a way that each duplicate consecutive element becomes a unique element. The first version uses operator== to compare the elements and the second version uses the given binary predicate pred. Syntax equality (1) template <class...
7 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