12. c++ STL tuple

2159 ワード

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
  • https://where-i-go.tistory.com/25?category=817926