pair<자료형1, 자료형2> 


두 자료형을 묶는다.


첫 번째 자료는 first, 두 번째 자료는 second로 접근


#include <utility> 에 있다.


단독 보다는 <algorithm>, <vector> 등의 헤더와 같이 사용하는데 이 헤더에 이미 include 하고 있어서 따로 include 안해도 된다.



생성 방법 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <algorithm>
 
using namespace std;
 
int main(void)
{
    pair<intint> p1;
    printf("%d %d\n", p1.first, p1.second);
    
    p1 = make_pair(1020);
    printf("%d %d\n", p1.first, p1.second);
 
    p1 = pair<intint>(3040);
    printf("%d %d\n", p1.first, p1.second);
 
    pair<intint> p2(5060);
    printf("%d %d\n", p2.first, p2.second);
 
    return 0;
}
 
 
cs




pair 안에 pair 자료형을 사용할 수도 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <algorithm>
 
using namespace std;
 
int main(void)
{
    pair<pair<intint>pair<int,int>> p1;
    p1 = make_pair(make_pair(1020), make_pair(3040));
    printf("%d %d\n%d %d\n", p1.first.first, p1.first.second, p1.second.first,p1.second.second);
 
    return 0;
}
cs



'컴퓨터공학 > STL' 카테고리의 다른 글

[STL]C++ STL priority_queue  (0) 2018.07.19
[STL]C++ STL queue  (0) 2018.07.19
[STL]C++ STL stack  (0) 2018.07.19
[STL]bitset  (0) 2018.07.04
[STL] C++ STL tuple  (0) 2018.06.22

+ Recent posts