습관처럼

C++ - Pair 본문

Language/C++

C++ - Pair

dev.wookii 2020. 6. 27. 12:50

이번에는 C++의 Pair 클래스에 대해 간단히 정리 해보려합니다. 클래스사용법, 함수 및 간단한 예제를 준비해봤습니다~

Pair 클래스란?

1. 두 객체를 하나의 객체로 취급 할 수 있게 묶어주는 클래스입니다.

2. STL에서 데이터 "쌍"을 표현할때 사용.

3. <utility> 헤더에 존재.

Pair 클래스 prototype

1. template <class T1, class T2> struct pair;

2. template <typename T1, typename T2> struct pair;
T1 : first T2 : second 로 구분합니다.

멤버 함수 및 간단한 사용법

1. pair<[type1], [type2]> p : 사용할 데이터 타입 1, 2를 넣고 그 타입의 pair 클래스인 p를 만듭니다.

2. p.first : p의 첫번째 인자를 반환해 줍니다.

3. p.second : p의 두번째 인자를 반환해 줍니다.
4. make_pair(변수1, 변수2) : 변수1과 변수2가 들어간 pair를 만들어줍니다.

5. operator로 (==, !=, <, >, <=, >=)가 정의 되어있어서, 사용이 가능합니다.

6. sort 알고리즘에 의해 정렬이 가능합니다.
(대소 비교 및 sort에의한 정렬에서 : 첫번째 인자 기준, 첫번째가 같으면 두번째인자로 판단)

int, string 인 경우 + 대소비교

#include<iostream>
#include<utility>
#include<string>
using namespace std;
 
void same(pair<int, string> a, pair<int, string> b){
    if(a == b){
        cout << "true" << endl;
    }else{
        cout << "false" << endl;
    }
}
void comp(pair<int, string> a, pair<int, string> b){
    if(a < b){
        cout << "true" << endl;
    }else{
        cout << "false" << endl;
    }
}
 
int main(void){
    pair<int, string> p1 = make_pair(1,"BlockDMask");    
    pair<int, string> p2 = make_pair(3,"Dok2");    
    pair<int, string> p3 = make_pair(1,"BlockDMask");    
 
    cout << "p1.first : " << p1.first << endl;
    cout << "p1.second : " << p1.second << endl;
    cout << endl;
    
    cout << "p1 == p2 ? ";
    same(p1, p2);
    cout << "p1 == p3 ? ";
    same(p1, p3);
    cout << endl;
    
    cout << "p1 < p2 ? ";
    comp(p1, p2);
    cout << "p1 < p3 ? ";
    comp(p1, p3);
    
    return 0;    
}
p1.first= 1
p1.second = BlockMask

p1 == p2 ? false
p1 == p3 ? true

p1 < p2 ? true
p1 < p3 ? false

vector 컨테이너의 타입으로 pair를 사용하는 경우. + 정렬(sort)

#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
 
int main(void){
    vector<pair<int, string> > v;
    
    v.push_back(pair<int, string>(3, "Dok2"));    
    v.push_back(pair<int, string>(6, "DMask"));    
    v.push_back(pair<int, string>(1, "Tiger JK"));    
    v.push_back(pair<int, string>(4, "Block"));    
    v.push_back(pair<int, string>(2, "banana"));    
    v.push_back(pair<int, string>(2, "apple"));    
    
    cout << "=== After sort === " << endl;
    
    vector<pair<int, string> >::iterator iter;
    for(iter = v.begin(); iter != v.end(); iter++){
        cout << "[" << iter->first << "," << iter->second << "]" << endl;
    }
    
    cout << endl;
    sort(v.begin(), v.end());
    cout << "=== After sort === " << endl;
    
    for(iter = v.begin(); iter != v.end(); iter++){
        cout << "[" << iter->first << "," << iter->second << "]" << endl;
    }
 
    return 0;    
}

> 앞에 있는 first 인자부터 정렬이 되고, 같으면 뒤에 인자 (string의 경우 사전순)로 정렬됨을 banana와 apple을 보고 판단할 수 있습니다. for문 루프 문에서는 i=0~ v.size()로 설정하셔도 되지만 다른 방법도 있음을 알려드리기 위해서 iterator을 사용했습니다~~

 

출처 : https://blockdmask.tistory.com/64

 

'Language > C++' 카테고리의 다른 글

C++ - map, unordered_map  (0) 2020.06.27
C++ - 문자 및 문자열 찾기  (0) 2020.06.27
C++ - Stack, Queue  (0) 2020.06.23
C++ - Quick Sort (feat Algorithm qsort())  (0) 2020.06.23
C++ - 중복제거  (0) 2020.06.23