首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::tie

Defined in header <tuple>

?

?

template< class... Types > tuple<Types&...> tie( Types&... args );

?

(since C++11) (until C++14)

template< class... Types > constexpr tuple<Types&...> tie( Types&... args );

?

(since C++14)

创建对其参数或实例的lvalue引用的元组。std::ignore...

参数

args

-

zero or more lvalue arguments to construct the tuple from

返回值

std::tuple对象,其中包含lvalue引用。

例外

noexcept规格:

noexcept

注记

std::tie可用于解压std::pair因为std::tuple有一个转换分配成对:

二次

代码语言:javascript
复制
bool result;
std::tie(std::ignore, result) = set.insert(value);

二次

std::tie可用于将词典编纂比较引入到结构中,或用于解压元组:

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <set>
#include <tuple>
 
struct S {
    int n;
    std::string s;
    float d;
    bool operator<(const S& rhs) const
    {
        // compares n to rhs.n,
        // then s to rhs.s,
        // then d to rhs.d
        return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
    }
};
 
int main()
{
    std::set<S> set_of_s; // S is LessThanComparable
 
    S value{42, "Test", 3.14};
    std::set<S>::iterator iter;
    bool inserted;
 
    // unpacks the return value of insert into iter and inserted
    std::tie(iter, inserted) = set_of_s.insert(value);
 
    if (inserted)
        std::cout << "Value was inserted successfully\n";
}

二次

产出:

二次

代码语言:javascript
复制
Value was inserted successfully

二次

make_tuple

creates a tuple object of the type defined by the argument types (function template)

forward_as_tuple

creates a tuple of rvalue references (function template)

tuple_cat

creates a tuple by concatenating any number of tuples (function template)

ignore

placeholder to skip an element when unpacking a tuple using tie (constant)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com