Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

How to use the unique_copy() function in C++

Harsh Jain

The unique_copy() function is available in the <algorithm> library in C++. The unique_copy() function copies the sequence so that no consecutive duplicate elements are copied in the sequence.

Syntax

Here are the function prototypes:

template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result);  

                                 OR

template <class InputIterator, class OutputIterator, class BinaryPredicate>  
OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate function);  

Parameters

The unique_copy() method accepts the parameters mentioned below:

  • first: The forward iterator for the initial position of the derived range.

  • last: The forward iterator for the final position of the derived range.

  • result: The forward iterator for the initial position of the range where the unique copied elements will be stored.

  • function: A user-defined binary function that defines the condition to be satisfied if two elements in a range are taken as equivalent and removed in the copied elements. This is an optional parameter.

Return value

It returns an output iterator pointing to the position of the first element in the copied range, receiving the copy with only unique elements.

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool myfunction (int i, int j) {
  return (i==j);
}

int main()
{
  vector<int> v = { 10,10,30,30,30,100,10,300,300,70,70,80};
  vector<int> vec(10);
  vector<int>::iterator ip;

  ip = unique_copy(v.begin(), v.begin() + 12, vec.begin());
  
  vec.resize(distance(vec.begin(), ip));

  cout << "Vector before using unique_copy(): ";
  for (ip = v.begin(); ip != v.end(); ++ip)
    cout << *ip << " ";
  
  cout<<endl;
  cout << "Copy of vector after using unique_copy(): ";
  for (ip = vec.begin(); ip != vec.end(); ++ip)
    cout << *ip << " ";
  
  ip = unique_copy(v.begin(), v.begin() + 12, vec.begin(), myfunction);
  vec.resize(distance(vec.begin(), ip));

  cout<<endl;
  cout << "Copy of vector after using unique_copy() with optional parameter: ";
  for (ip = vec.begin(); ip != vec.end(); ++ip)
    cout << *ip << " ";
  return 0;
}
Use unique_copy() function in C++

Explanation

  • In lines 1 to 3, we import the required header files.

  • In lines 6 to 8, we create a binary function that returns true if both the elements are the same. We will use this function to pass it as a parameter to the unique_copy() function.

  • In line 10, we make a main() function.

  • In line 12, we define a vector of the int type.

  • In line 13, we declare a new vector of the int type.

  • In line 14, we declare an iterator to a vector.

  • In line 16, we use the unique_copy() function to get the vector with unique elements and store it in the new vector.

  • In line 18, we resize the new vector.

  • In line 20, we display the message regarding the original vector.

  • In lines 21 and 22, we display the original vector.

  • In line 25, we display the message regarding the copied vector with unique elements after using the unique_copy() function.

  • In lines 26 and 27, we display the copied vector with unique elements.

  • In line 29, we call the unique_copy() function and pass the optional parameter, i.e., the binary function which specifies when to consider the two elements equivalent.

  • In lines 34 to 35, we print the copied vector.

Note: There is no change in the output as the binary function that we created is working in a default manner (similar to the == operator). However, we can change the conditions inside that binary function if we want to customize the condition for two elements to be equivalent.

In this way, we can use the unique_copy() function to create a copy of a sequence of elements with unique elements in C++.

RELATED TAGS

c++
communitycreator
RELATED COURSES

View all Courses

Keep Exploring