12. c++ STL tuple

han811·2021년 4월 28일
0

c++

목록 보기
12/14
post-thumbnail

1. get

#include <iostream>
#include <tuple>
using namespace std;

int main(void)
{
	tuple<int,int,int> t1 = make_tuple(1,2,3);
    
    cout << get<0>(t1) << '\n';
    cout << get<1>(t1) << '\n';
    cout << get<2>(t1) << '\n';
    
	return 0;
}
  • 여러개의 값들을 묶을 수 있는 tuple 입니다.
  • get을 이용하여 원소에 접근합니다.

2. tie

#include <iostream>
#include <tuple>
using namespace std;

int main(void)
{
	auto t = make_tuple(1,2,3);
    int x = get<0>(t);
    int y = get<1>(t);
    int z = get<2>(t);
    
    cout << x << ' ' << y << ' ' << z << '\n';
    
    x=y=z=0;
    cout << x << ' ' << y << ' ' << z << '\n';
    
    tie(x,y,z) = t;
    cout << x << ' ' << y << ' ' << z << '\n';
    
    x=y=z=0;
    tie(x,y,ignore) = t;
    cout << x << ' ' << y << ' ' << z << '\n';
    
	return 0;
}
  • pair 혹은 tuple과 같이 사용할 때 유용한 방법입니다.
  • 묶인 값들을 tie 안의 변수들에 순차적으로 대입하여 줍니다.

reference

profile
han811

0개의 댓글