Doing/C&C++
[C++] Pair, Vector STL
YongArtist
2020. 6. 29. 08:47
STL
표준 템플릿 라이브러리(Standard Template Library)
C++에서 필요한 자료구조와 알고리즘을 Template로 제공하는 라이브러리
구성
Container, Iterator, functor, Algorith
Pair and Vector
Pair
#include <utility> 헤더 파일을 추가해주거나 <vector>또는 <algorithm> 헤더를 추가해줍니다.
Pair는 두 쌍의 자료형을 묶어서 사용하는 것으로 pair<int, int>형식으로 작성한다.
#include <iostream>
#include <vector>
using namespace std;
pair<int, int> p;
int main(){
// how to put in pair
// p.first = 1;
// p.second = 5;
p = make_pair(1,5);
printf("%d %d\n", p.first, p.second);
return 0;
}
pair 값 설정은 두 가지 방법으로
-
first와 second로 접근해서 직접 값을 넣기
-
make_pair(value, value)
Vector
배열을 동적으로 사용하는 것으로 쉽게 변하는 배열이라고 생각하면 된다.
#include <vector>
vector에서 멤버함수 여러 가지 있다.
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int>> v(2);
int main(){
v[0] = make_pair(2,6);
printf("%d %d\n", v[0].first, v[0].second);
printf("%zu \n",v.size());
v.resize(3);
printf("%zu \n",v.size());
return 0;
}