2개의 데이터를 묶을 땐 pair를 사용하면 됐다.

그런데 leetcode hard단계를 풀다 보니 3개 묶는일이 많아지더라.

주로 tuple형태로 return되는 값을 받을 때 사용한다.

  ! c++ 11버전 이상에서만 사용가능하다

 

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

int main() {
    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';    //1 2 3

    x = y = z = 0;
    cout << x << ' ' << y << ' ' << z << '\n';    //0 0 0
    
    tie(x, y, z) = t;
    cout << x << ' ' << y << ' ' << z << '\n';    //1 2 3

    x = y = z = 0;
    tie(x, y, ignore) = t;    //세번째 자리는 무시 : ignore
    cout << x << ' ' << y << ' ' << z << '\n';    //1 2 0

    return 0;
}

 

다음은 cpp reference url : https://en.cppreference.com/w/cpp/utility/tuple/tie

'C++ STL' 카테고리의 다른 글

[C++][STL] fill, fill_n  (0) 2022.03.01
[C++][STL] set과 unordered_set의 차이  (0) 2021.11.11
[C++][STL] Hash Set  (0) 2021.10.21
[C++][STL] sort  (0) 2021.10.04
[C++][STL] Queue  (0) 2021.09.26

+ Recent posts