std::tie
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <tuple> 中定义
|
||
template< class... Types > tuple<Types&...> tie( Types&... args ); |
(C++11 起) | |
创建一个元组的左值的引用给它的参数或实例std::ignore
原文:
Creates a tuple of lvalue references to its arguments or instances of std::ignore.
目录 |
[编辑] 参数
args | - | 零个或多个左值的参数来构造的元组
原文: zero or more lvalue arguments to construct the tuple from |
[编辑] 返回值
一个
std::tuple
对象,其中包含左值referenes.原文:
A
std::tuple
object containing lvalue referenes.[编辑] 例外
[编辑] 示例
std::tie可以用来引入一个结构逐一比较,可以把一个元组:
原文:
std::tie can be used to introduce lexicographical comparison to a struct or to unpack a tuple:
#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 sucessfully\n"; }
输出:
Value was inserted sucessfully
创建一个 tuple 对象的参数类型定义的类型原文: creates a tuple object of the type defined by the argument types(函数模板) | |
创建一个 tuple 的右值引用原文: creates a tuple of rvalue references(函数模板) | |
通过连接任意数量的元组来创建一个tuple (函数模板) | |
当使用tie拆开一个tuple 时用来跳过元素的占位符 (常量) |