栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++

C++_3——库(tuple)

  这一系列文章的目的是在学习了C++基础后,继续补充一些C++基础和进阶的知识点,包括C++11的相关内容。
以C++11标准为基础。

C++网站:http://www.cplusplus.com/reference/

    tuple类似于C语言的结构体,tuple访问成员不用变量名而是变量顺序,tuple内的变量类型可不同基本使用
    #include  
    // 构造
    std::tuple first;                             // default
    std::tuple second (first);                    // copy
    std::tuple third (std::make_tuple(20,'b'));   // move
    std::tuple fourth (third);                   // implicit conversion
    std::tuple fifth (10,'a');                    // initialization
    std::tuple sixth (std::make_pair(30,'c'));    // from pair / move
    
    // std::make_tuple
    auto first = std::make_tuple (10,'a');             		// tuple < int, char >
    
    const int a = 0; int b[3];                         		// decayed types:
    auto second = std::make_tuple (a,b);               		// tuple < int, int* >
    
    auto third = std::make_tuple (std::ref(a),"abc");  		// tuple < const int&, const char* >
    
    // std::tie
    int myint;
    char mychar;
    
    std::tuple mytuple;
    mytuple = std::make_tuple (10, 2.6, 'a');          // packing values into tuple
    std::tie (myint, std::ignore, mychar) = mytuple;   // unpacking tuple into variables
    
    // std::get 这里返回的是元素的引用
    std::tuple mytuple (10,'a');
    std::get<0>(mytuple) = 20;
    std::cout << std::get<0>(mytuple) << " and " << std::get<1>(mytuple); // 20 and a
    
    // std::tuple_cat 串联不同的tuple
    std::tuple mytuple (3.14,"pi");
    std::pair mypair (10,'a');
    auto myauto = std::tuple_cat ( mytuple, std::tuple(mypair) );
    
    其他
      std::ref与std::cref的使用,参见这篇博客
      std::ref为引用传递,std::cref为const引用传递std::ignore
      一方面是作为std::tie的占位符,另一方面还可以避免一些未使用函数返回值的warning,如下,nodiscard用于提示这个函数的返回值很重要,不应丢弃,一旦丢弃则发出自定义的字符串以警告(“important…info output”),但如果使用std::ignore,则没有警告。
    #include 
     
    [[nodiscard("important...info output")]] int dontIgnoreMe(){
        return 42;
    }
     
    int main(){
        std::ignore = dontIgnoreMe();
    }
    
     **注. nodiscard是C++17的内容
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号