C++ std::unique_copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::unique_copy() copies a range to a destination and keeps only the first of each group of equal neighbours. The source is not changed.

Like unique(), it only compares neighbours, so sort first if you want every duplicate gone.

Syntax

Add #include <algorithm>, then call std::unique_copy(). Start with these two lines.

C++
std::unique_copy(src.begin(), src.end(), std::back_inserter(out));
std::unique_copy(src.begin(), src.end(), out.begin(), pred);
CallWhat it does
std::unique_copy(src.begin(), src.end(), std::back_inserter(out));Copy while collapsing runs of equal neighbours.
std::unique_copy(src.begin(), src.end(), out.begin(), pred);Use your own rule for "equal".
Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt unique_copy(InputIt first, InputIt last, OutputIt d_first);

template<class InputIt, class OutputIt, class BinaryPred>
OutputIt unique_copy(InputIt first, InputIt last,
                     OutputIt d_first, BinaryPred p);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_firstRequired. Where the copy starts.
pOptional. A function that says if two neighbours count as equal.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Collapse runs

Copy a vector while collapsing equal neighbours.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> src = {1, 1, 2, 2, 3, 1, 1};
    std::vector<int> out;

    std::unique_copy(src.begin(), src.end(), std::back_inserter(out));

    std::cout << "src size: " << src.size() << '\n';
    std::cout << "out: ";
    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The runs are 1 1, 2 2, 3 and 1 1.
  2. One value from each run goes into out.
  3. The last 1 stays because it is not next to the first run.

Example 2 — Squeeze letters

Remove repeated letters from a word.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main() {
    std::string word = "bookkeeper";
    std::string result;

    std::unique_copy(word.begin(), word.end(), std::back_inserter(result));

    std::cout << result << '\n';
    return 0;
}

How It Works

  1. oo, kk and ee are runs of equal letters.
  2. Each run is shortened to one letter.
  3. The later single e is not next to the first run, so it stays in the output.

Example 3 — Print directly

Keep the first word for each starting letter and print it.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"apple", "avocado", "banana", "blueberry", "cherry"};

    std::unique_copy(words.begin(), words.end(),
                     std::ostream_iterator<std::string>(std::cout, " "),
                     [](const std::string& a, const std::string& b) { return a[0] == b[0]; });
    std::cout << '\n';
    return 0;
}

How It Works

  1. The predicate says two words are equal when they start with the same letter.
  2. avocado follows apple, and blueberry follows banana, so they are skipped.
  3. std::ostream_iterator sends the kept words straight to the screen.

Frequently Asked Questions

In C++, std::unique_copy() copies a range while collapsing consecutive duplicate elements. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the position after the last element written in the destination.
unique() changes the original range. unique_copy() writes the result to a destination and leaves the source alone.
Only if you want every duplicate removed. unique_copy() only collapses neighbouring duplicates.

Next: upper_bound()

Finds the first element that is greater than a value in a sorted range.

upper_bound() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful