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


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

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

    元组std::tuple可以将不同类型的元素存放在一起,可以理解为std::pair的扩展(pair只能包含两个元素,而tuple可以多个)。
    std::tuple拥有从 pair 的转换赋值,因为std::tuple的实现中重载了操作符=,其部分原型如下:

    1. template< class U1, class U2 >
    2. tuple& operator=( const std::pair& p );//C++11 起, C++20 前

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

    1. #include
    2. #include
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6.     std::set<int> sets;
    7.     std::set<int>::iterator iter;
    8.     bool result = false;
    9.     std::tie(iter, result) = sets.insert(1);//解包insert的返回值为iter与result
    10.     std::tie(std::ignore, result) = sets.insert(2); // std::ignore是std::tie在解包时作为不使用的参数的占位符使用,即忽略某些tuple中的某些返回值。
    11.     std::cout << result << std::endl; // 输出1
    12.     return 0;
    13. }

    std::set的insert函数原型如下:

    1. std::pairbool> insert( const value_type& value );
    2. std::pairbool> insert( value_type&& value );
    3. template< class InputIt >
    4. void insert( InputIt first, InputIt last );
    5. void insert( std::initializer_list ilist );

    为生成pair, c++ 提供了make_pair的快捷操作,相应的,对tuple也提供了make_tuple用于快速创建tuple对象。创建tuple对象的方式有三种:

    1. std::tuple<int, double, std::string> student1 = { 1, 77.7, "Sunny" }; // 定义时 初始化
    2. std::tuple<int, double, std::string> student2 ( 2, 88.8, "Gavin" );   // 使用构造函数
    3. auto student3 = std::make_tuple(3, 99.9, "Lucia" );                   // 使用make_tuple

    使用std::tie解包tuple

    1. #include
    2. #include
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6.     auto student3 = std::make_tuple(3, 99.9, "Lucia" );
    7.     int id;
    8.     std::string name;
    9.     std::tie(id, std::ignore, name) = student3;
    10.     std::cout << "student id: " << id << "  \t " << "student name: " << name << std::endl;
    11.     return 0;
    12. }


    可以将结构体成员传入std::tie,从而实现结构体的比较。

    1. struct Student {
    2.     int id;
    3.     float score;
    4.     std::string name;
    5.     bool operator<(const Student& rhs) const
    6.     {
    7.         // 先比较id与rhs.id
    8.         // 然后比较score与rhs.score
    9.         // 最后比较name与rhs.name
    10.         // tuple内已经重载了运算符<
    11.         return std::tie(id, score, name) < std::tie(rhs.id, rhs.score, rhs.name);
    12.     }
    13. };

    一个例子:

    1. #include
    2. #include
    3. #include
    4. #include
    5. struct Student {
    6.     int id;
    7.     float score;
    8.     std::string name;
    9.     bool operator<(const Student& rhs) const
    10.     {
    11.         // 先比较id与rhs.id
    12.         // 然后比较score与rhs.score
    13.         // 最后比较name与rhs.name
    14.         // tuple内已经重载了运算符<
    15.         return std::tie(id, score, name) < std::tie(rhs.id, rhs.score, rhs.name);
    16.     }
    17. };
    18. int main(int argc, char *argv[])
    19. {
    20.     std::set sets;
    21.     Student student1{1, 77.7, "Sunny"};
    22.     Student student2{ 2, 88.8, "Gavin"};
    23.     std::set::iterator iter;
    24.     bool result = false;
    25.     std::tie(iter, result) = sets.insert(student1);
    26.     if (result)
    27.     {
    28.         std::cout << "student1 was inserted successfully" <
    29.     }
    30.     std::tie(std::ignore, result) = sets.insert(student2); // 使用std::ignore忽略insert的返回pair中的第一个元素
    31.     if (result)
    32.     {
    33.         std::cout << "student2 was inserted successfully" <
    34.     }
    35.     result = student1 < student2;
    36.     std::cout << "student1 < student2: " << result << std::endl;
    37.     return 0;
    38. }

    原文链接:https://blog.csdn.net/caoshangpa/article/details 

  • 相关阅读:
    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