Gidhub BE Developer

Vector에서 중복 값 삭제하기

2018-10-11
goodGid

To Do

  • Vector안에서 중복된 값을 삭제해보자.

Code

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

int main() {
    vector<int> v;
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(3);
    v.push_back(2);
    v.push_back(1);
    
    for(int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    for(int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    
    return 0;
}
1 1 2 3 3 2 1 
1 2 3 
  • 방법은 굉장히 간단하다.

  • sort와 unique 사용을 위해서 algorithm 헤더를 선언한 후

  • sort() -> v.erase(unique(v.begin(),v.end()), v.end())만 하면 끝 !

  • 물론 int 타입뿐 아니라 다른 타입도 가능하다.

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

int main() {
    vector<string> v;
    v.push_back("a");
    v.push_back("a");
    v.push_back("B");
    v.push_back("b");
    v.push_back("A");
    v.push_back("b");
    v.push_back("F");
    
    for(int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    for(int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    
    return 0;
}
a a B b A b F 
A B F a b 

Reference


Index