C++11新特性之十六:std::tie

在c++ 11标准库中,加入了std::tie,在c++ 14中改进,方便使用。 其与std::tuple关系密切, 主要目的是方便地使用std::tuple。
std::tie函数的作用就是从元素引用中生成一个std::tuple元组,其在头文件<tuple>中定义,其函数原型如下:

template< class... Types >
std::tuple<Types&...> tie( Types&... args ) noexcept; //C++11起, C++14前

template< class... Types >
constexpr std::tuple<Types&...> 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<U1, U2>& p );//C++11 起, C++20 前

因此,std::tie可以用于pair的解包

#include <set>
#include <tuple>
#include <iostream>

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<iterator, bool> insert( const value_type& value );
std::pair<iterator, bool> insert( value_type&& value );

template< class InputIt >
void insert( InputIt first, InputIt last );
void insert( std::initializer_list<value_type> 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 <tuple>
#include <iostream>
#include <string>

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 <tuple>
#include <set>
#include <iostream>
#include <string>

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<Student> sets;
    Student student1{1, 77.7, "Sunny"};
    Student student2{ 2, 88.8, "Gavin"};

    std::set<Student>::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 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草上爬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值