在c++ 11标准库中,加入了std::tie,在c++ 14中改进,方便使用。 其与std::tuple关系密切, 主要目的是方便地使用std::tuple。
std::tie函数的作用就是从元素引用中生成一个std::tuple元组,其在头文件
- template< class... Types >
- std::tuple
tie( Types&... args ) noexcept ; //C++11起, C++14前 -
- template< class... Types >
- constexpr std::tuple
tie( Types&... args ) noexcept; //C++14起
元组std::tuple可以将不同类型的元素存放在一起,可以理解为std::pair的扩展(pair只能包含两个元素,而tuple可以多个)。
std::tuple拥有从 pair 的转换赋值,因为std::tuple的实现中重载了操作符=,其部分原型如下:
- template< class U1, class U2 >
- tuple& operator=( const std::pair
& p );//C++11 起, C++20 前
因此,std::tie可以用于pair的解包
- #include
- #include
- #include
-
- int main(int argc, char *argv[])
- {
- std::set<int> sets;
- std::set<int>::iterator iter;
- bool result = false;
- std::tie(iter, result) = sets.insert(1);//解包insert的返回值为iter与result
- std::tie(std::ignore, result) = sets.insert(2); // std::ignore是std::tie在解包时作为不使用的参数的占位符使用,即忽略某些tuple中的某些返回值。
- std::cout << result << std::endl; // 输出1
-
- return 0;
- }
std::set的insert函数原型如下:
- std::pair
bool > insert( const value_type& value ); - std::pair
bool > insert( value_type&& value ); -
- template< class InputIt >
- void insert( InputIt first, InputIt last );
- void insert( std::initializer_list
ilist ) ;
为生成pair, c++ 提供了make_pair的快捷操作,相应的,对tuple也提供了make_tuple用于快速创建tuple对象。创建tuple对象的方式有三种:
- std::tuple<int, double, std::string> student1 = { 1, 77.7, "Sunny" }; // 定义时 初始化
- std::tuple<int, double, std::string> student2 ( 2, 88.8, "Gavin" ); // 使用构造函数
- auto student3 = std::make_tuple(3, 99.9, "Lucia" ); // 使用make_tuple
使用std::tie解包tuple
- #include
- #include
- #include
-
- int main(int argc, char *argv[])
- {
- auto student3 = std::make_tuple(3, 99.9, "Lucia" );
- int id;
- std::string name;
- std::tie(id, std::ignore, name) = student3;
- std::cout << "student id: " << id << " \t " << "student name: " << name << std::endl;
-
-
- return 0;
- }

可以将结构体成员传入std::tie,从而实现结构体的比较。
- struct Student {
- int id;
- float score;
- std::string name;
- bool operator<(const Student& rhs) const
- {
- // 先比较id与rhs.id
- // 然后比较score与rhs.score
- // 最后比较name与rhs.name
- // tuple内已经重载了运算符<
- return std::tie(id, score, name) < std::tie(rhs.id, rhs.score, rhs.name);
- }
- };
一个例子:
- #include
- #include
- #include
- #include
-
- struct Student {
- int id;
- float score;
- std::string name;
- bool operator<(const Student& rhs) const
- {
- // 先比较id与rhs.id
- // 然后比较score与rhs.score
- // 最后比较name与rhs.name
- // tuple内已经重载了运算符<
- return std::tie(id, score, name) < std::tie(rhs.id, rhs.score, rhs.name);
- }
- };
-
- int main(int argc, char *argv[])
- {
- std::set
sets; - Student student1{1, 77.7, "Sunny"};
- Student student2{ 2, 88.8, "Gavin"};
-
- std::set
::iterator iter; - bool result = false;
-
- std::tie(iter, result) = sets.insert(student1);
- if (result)
- {
- std::cout << "student1 was inserted successfully" <
- }
-
- std::tie(std::ignore, result) = sets.insert(student2); // 使用std::ignore忽略insert的返回pair中的第一个元素
- if (result)
- {
- std::cout << "student2 was inserted successfully" <
- }
-
- result = student1 < student2;
- std::cout << "student1 < student2: " << result << std::endl;
-
-
- return 0;
- }

-
相关阅读:
umi首屏加载速度优化
中仑网络全站 Dubbo 2 迁移 Dubbo 3 总结
网络个各种协议
.NET下数据库的负载均衡(有趣实验)
JDBC与正则化
零基础入门初学 Python 需要安装哪些软件?
19 C++设计模式之中介者(Mediator)模式
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
80行代码轻松搞定反向传播神经网络(BPNN)
等保测评FAQ
-
原文地址:https://blog.csdn.net/caoshangpa/article/details/134002467