建站公司的服务器,免费制作网页平台,黄页营销网站,建设河南网站在c 11标准库中#xff0c;加入了std::tie#xff0c;在c 14中改进#xff0c;方便使用。 其与std::tuple关系密切#xff0c; 主要目的是方便地使用std::tuple。 std::tie函数的作用就是从元素引用中生成一个std::tuple元组#xff0c;其在头文件tuple中定义 11标准库中加入了std::tie在c 14中改进方便使用。 其与std::tuple关系密切 主要目的是方便地使用std::tuple。 std::tie函数的作用就是从元素引用中生成一个std::tuple元组其在头文件tuple中定义其函数原型如下
template class... Types
std::tupleTypes... tie( Types... args ) noexcept; //C11起, C14前template class... Types
constexpr std::tupleTypes... tie( Types... args ) noexcept; //C14起
元组std::tuple可以将不同类型的元素存放在一起可以理解为std::pair的扩展pair只能包含两个元素而tuple可以多个。 std::tuple拥有从 pair 的转换赋值因为std::tuple的实现中重载了操作符其部分原型如下
template class U1, class U2
tuple operator( const std::pairU1, U2 p );//C11 起, C20 前
因此std::tie可以用于pair的解包
#include set
#include tuple
#include iostreamint main(int argc, char *argv[])
{std::setint sets;std::setint::iterator iter;bool result false;std::tie(iter, result) sets.insert(1);//解包insert的返回值为iter与resultstd::tie(std::ignore, result) sets.insert(2); // std::ignore是std::tie在解包时作为不使用的参数的占位符使用即忽略某些tuple中的某些返回值。std::cout result std::endl; // 输出1return 0;
}
std::set的insert函数原型如下
std::pairiterator, bool insert( const value_type value );
std::pairiterator, bool insert( value_type value );template class InputIt
void insert( InputIt first, InputIt last );
void insert( std::initializer_listvalue_type ilist );
为生成pair, c 提供了make_pair的快捷操作相应的对tuple也提供了make_tuple用于快速创建tuple对象。创建tuple对象的方式有三种
std::tupleint, double, std::string student1 { 1, 77.7, Sunny }; // 定义时 初始化
std::tupleint, double, std::string student2 ( 2, 88.8, Gavin ); // 使用构造函数
auto student3 std::make_tuple(3, 99.9, Lucia ); // 使用make_tuple
使用std::tie解包tuple
#include tuple
#include iostream
#include stringint 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 tuple
#include set
#include iostream
#include stringstruct 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::setStudent sets;Student student1{1, 77.7, Sunny};Student student2{ 2, 88.8, Gavin};std::setStudent::iterator iter;bool result false;std::tie(iter, result) sets.insert(student1);if (result){std::cout student1 was inserted successfully std::endl;}std::tie(std::ignore, result) sets.insert(student2); // 使用std::ignore忽略insert的返回pair中的第一个元素if (result){std::cout student2 was inserted successfully std::endl;}result student1 student2;std::cout student1 student2: result std::endl;return 0;
} 原文链接https://blog.csdn.net/caoshangpa/article/details