C++ std::tuple
阿新 • • 發佈:2018-12-17
std::tuple
是一個可以支援引數集的類模板
- std::make_tuple 建立一個 tuple
- std::tie 建立一個左值引用的 tuple
- std::get 獲取引數集合某個位置的引數
- tuple_size 獲取引數集數目
示例:
#include <iostream> #include <vector> #include <tuple> #include <string> constexpr int N = 10; template<class T> void Print(T t) { std::cout << std::tuple_size<T>::value << "\n"; } int main(){ using TP = std::tuple<bool, int , float>; std::vector<TP> v; v.reserve(N); for (int i = 0; i < N; ++i) v.push_back(TP{false , 1 , 6.66}); for (int i = 0; i < N; ++i) v.push_back(std::make_tuple(true, 1, 6.66)); for (auto x : v) { std::cout << std::get<0>(x) << " " << std::get<1>(x) << " " << std::get<2>(x) << "\n"; } int n = 5; std::string s = "sss"; auto tp = std::tuple_cat(v[0], std::tie(n , s)); std::cout << std::get<0>(tp) << " " << std::get<1>(tp) << " " << std::get<2>(tp) << " " << std::get<3>(tp) << " " << std::get<4>(tp) << " " << "\n"; Print(tp); return 0; }