在线文档教程
C++
应用 | Utilities

std::tie

STD:TIE

Defined in header
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有一个转换分配成对:

二次

bool result; std::tie(std::ignore, result) = set.insert(value

二次

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

二次

#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"; }

二次

产出:

二次

Value was inserted successfully

二次

make_tuplecreates a tuple object of the type defined by the argument types (function template)
forward_as_tuplecreates a tuple of rvalue references (function template)
tuple_catcreates a tuple by concatenating any number of tuples (function template)
ignoreplaceholder to skip an element when unpacking a tuple using tie (constant)

© cppreference.com

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

http://en.cppreference.com/w/cpp/实用程序/tuple/tie