C++ unique Function Guide

The `unique` function in C++ is used to remove adjacent duplicate elements in a container and returns an iterator pointing to the new logical end. It moves the adjacent duplicate elements in the container to the end and returns an iterator pointing to the new logical end.

The method of using the unique function is as follows:

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5};
    
    auto it = std::unique(vec.begin(), vec.end());
    
    vec.erase(it, vec.end());
    
    for (int i : vec) {
        std::cout << i << " ";
    }
    
    return 0;
}

In the example above, we use the unique function to remove adjacent duplicate elements from a vector that contains repeated elements, and then output the deduplicated result. The unique function moves the adjacent duplicate elements to the end and returns an iterator pointing to the new logical end. We then use the erase function to delete the portion after the removed duplicates.

bannerAds