C++ Algorithm unique()30 Aug 2024 | 5 min read 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. SyntaxParameterfirst: A forward iterator pointing the position of the first element in the range to be scanned for duplicate removal. last: A forward iterator pointing the position one past the final element in the range to be scanned for duplicate removal. pred: A user-defined predicate function object that defines the condition to be satisfied if two elements in a range are to be taken as equivalent. A binary predicate returns two arguments and returns true when satisfied and false when not satisfied. 0 Return valueA forward iterator pointing to the new end of the range[first, last) that contains no consecutive duplicates. ComplexityComplexity is linear in the range [first, last): compares each pair of consecutive elements, and performs assignment operation on some of them. Data racesThe object in the range [first, last) are accessed and potentially modified. Exception safetyThis function throws an exception if any of pred, the element comparisons, the element assignments or the operations 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 unique(): Output: 1 2 3 4 5 6 7 Example 2Let's see another simple example: Output: myvector contains: 10 20 30 20 10 Example 3Let's see another simple example: Output: unsorted unique : 2,5,3,1,2,4,2,1,4,3 sorted unique : 1,2,3,4,5 Example 4Let's see another simple example: Output: 1 2 3 4 5 6 7 wanna go to space? Example 5Let's see another example: Output: Vector v1 is ( 5 -5 5 -5 5 -5 5 -5 4 4 4 4 7 ). Removing adjacent duplicates from vector v1 gives ( 5 -5 5 -5 5 -5 5 -5 4 7 ). Removing adjacent duplicates from vector v1 under the binary predicate mod_equal gives ( 5 4 7 ). Removing adjacent elements satisfying the binary predicate mod_equal from vector v1 gives ( 5 7 ). Next TopicC++ Algorithm |
algorithm find_end() function
C++ Algorithm Function find_end () C++ Algorithm find_end()function searches for the last occurrence of a pattern in the container, or say the last occurrence of a small portion of the sequence in the container. It basically searches the range specified by [first1,last1)for the occurrence of sequence which...
2 min read
algorithm partial_sort_copy() function
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
algorithm sort() function
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
algorithm for_each() function
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
algorithm move() function
C++ Algorithm Function move () C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntax template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is...
2 min read
algorithm copy() function
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
algorithm lower_bound() function
C++ Algorithm lower_bound() C++ Algorithm lower_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in an ordered range [first, last) that is not less than (i.e. greater than or equal to) to the specified value...
4 min read
algorithm swap_ranges() function
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
algorithm all_of() function
C++ Algorithm Functions all_of() C++ Algorithm all_of() function returns a true value if the value of 'pred' argument is true. The value should be true for all elements in the range [first, last]. Syntax template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies the...
1 min read
algorithm copy_if() function
C++ Algorithm Function copy_if() C++ Algorithm copy_if() function is used to copy the elements of the container [first,last] into a different container starting from result for which the value of pred is true. Syntax template<class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result,UnaryPredicate pred); Parameter first: It is...
1 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