STL之Tuple

5.1.2 Tuple(不定值的数组)

  tuple的模板参数可以是任意的,带来巨大的便利性。其在头文件<tuple>内。定义如下:

namespace std
{
    template<typename... Types>
    class tuple;
}

  需要注意的几个函数如下:

std::tie(agrs...)    该函数返回的是args的引用的tuple

tuple_size<idx,

tupletype>::value

获得tuple元素的个数

tuple_element<idx,

tupletype>::type

取得第idx元素的类型
tuple_cat(t1,t2...) 将tuple连接起来,原有元素的性质不变,引用的性质还是引用的性质

注意:tuple<idx>get(Type) 的idx不能在运行时输入,要在编译时就确定,即constexpr。另外还不支持初始化列initlist的默认转换。

有关例子如下:

int main()
{
    tuple<int, float, std::string> t(77, 1.1, "more light");
    int i;
    float f;
    string s;
    auto b=tie(i, f, s) = t;
    s = "hello";
    system("pause");
    return 0;
}

是b中的s=“hello”呢,还是t中的s=“hello”呢,答案是b中的。

猜你喜欢

转载自www.cnblogs.com/manch1n/p/10322120.html
今日推荐