unique を使ってみる (C++)
C++
Published: 2019-12-26

やったこと

to_string を使ってみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

この関数は、隣り合った重複要素を除いた要素を、範囲の先頭に集める。

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 1, 3, 2, 4, 5};
    sort(v.begin(), v.end());
    auto iter = unique(v.begin(), v.end());

    v.erase(iter, v.end());

    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << endl;;
    }
}

出力結果

1
2
3
4
5

参考