首页 > 编程语言 >C++17新特性

C++17新特性

时间:2024-08-10 21:38:29浏览次数:11  
标签:std 17 int auto c++ 特性 C++ byte

C++17新特性

语言特性

  1. 使用auto声明非类型模板参量

  2. 折叠表达式

    提供模板参数包的折叠

    template <typename... Args>
    bool logicalAnd(Args... args) {
        // 二元折叠
        return (true && ... && args);
    }
    bool b = false;
    bool& b2 = b;
    logicalAnd(b, b2, true); // == false
    
    template <typename... Args>
    auto sum(Args... args) {
        // 一元折叠
        return (... + args);
    }
    sum(5, 5.0, 7); // == 17
    
  3. 内联变量

  4. constexpr lambda

    使用constexpr完成编译时的lambda表达式

  5. 列表初始化推导新规则

    auto x = {1, 2, 3}; // deduct to std::initializer_list<int> before ,now deduct to int
    
  6. 嵌套命名空间

    这个特性使命名空间嵌套更加简洁:

    // before c++17
    namespace A{
        namespace B{
            namespace C{
                int i;
            }
        }
    }
    
    // c++17
    namespace A::B::C{
        int i;
    }
    
  7. 结构绑定

    这个特性可以用于解结构初始化,使用方式为auto [x,y,z] = expr;expr为类元组对象时,其中的元素将对应绑定到x,y,z。类元组对象包括std::pair, std::turple, std::array等。

    // first example
    using Pair = std::pair<int, int>;
    Pair init(){
        return Pair{0, 0};
    }
    
    const auto [x, y] = init();
    x; // x == 0
    y; // y == 0
    
    // second example
    using u_map = std::unordered_map<std::string, int>;
    u_name mapping{
        {"a", 1},
        {"b", 2},
        {"c", 3}
    };
    
    // destructure by reference
    for (const auto [key, value] : mapping){
        cout << key << ":" << value << endl;
    }
    
  8. 选择语句初始化器

    • if initializer

      // before c++17
      std::mutex mtx;
      {
          std::lock_guard<std::mutex> lk(mtx);
          if (v.empty()) v.push(val);
      }
      
      // c++17
      std::mutex mtx;
      {
          if (std::lock_guard<std::mutex> lk(mtx); v.empty()){
              v.push(val);
          }
      }
      
      
    • switch initializer

      // before c++17
      A apple(args);
      switch(auto s = apple.stat()){
      	// body  
      }
      
      // c++17
      switch(A apple(args); auto s = apple.stat()){
      	// body
      }
      
  9. constexpr if

    用于编写依赖于编译时条件实例化的代码

  10. UTF-8 字面字符

    char c = u8'a';
    
  11. 枚举列表直接初始化

    枚举现在能够使用列表进行初始化

    enum byte: unsigned char{};
    byte a = {0};
    byte d = byte{256};
    
  12. [[nodiscard]] [[fallthrough]] [[maybe_unused]] 属性

  13. __has_include宏操作符

    这个特性可以用来检查库是否可以被引入。

    #ifdef __has_include
    #if __has_include(<iostream>) // 如果能够引入,返回true
    #include <iostream>
    #define have_iostream 1
    #else
    #define have_iostream 0
    #endif
    
  14. 类模板参数推导(CTAD

    这个特性,使类模板的参数也能够进行推导

    std::vector nums{1, 2, 3}; // deduct to std::vector<int>
    
    std::mutex mtx;
    auto lk = std::lock_guard{mtx}; // deduct to std::lock_guard<std::mutex>
    
    auto p = std::pair{0.2, 0.3}; // deduct to std::pair<int, int>
    

库特性

  1. std::variants

    相当于类型安全的union,同时只能存在一个值

    std::variant<int, int, double>  v{1.20};
    std::get<double>(v); // 1.20
    std::get<2>(v); // 1.20
    
  2. std::optional

    std::optional<std::string> create(bool b) {
      if (b) {
        return "cts";
      } else {
        return {};
      }
    }
    
    create(false).value_or("mx"); // == "mx"
    create(true).value(); // == "cts"
    
  3. std::any

    类型安全的容器,存放任意类型的单值。

    std::any x{1};
    any_cast<int>(x);
    std::any_cast<int&>(x) = 10; // x==10
    int s = any_cast<int>(x); // 这一步会将x存储的值转换为一个左值。
    
  4. std::string_view

    非所有权字符串引用

  5. std::invoke

    唤醒一个有参数的可调用对象

  6. std::apply

    唤醒一个有参数元组的可调用对象

  7. std::filesystem

    提供操作文件系统目录、文件和路径的标准方式。

  8. std::byte

    提供一个以字节表示数据的标准方式,与charunsigned char相比的优点是byte对象非字符类型和算术类型,只能够使用位操作。

    std::byte x {0};
    std::byte y {0xAA};
    std::byte z = x & y;
    int i = std::to_integer<int>(z); // 0
    
  9. mapset的分片

  10. 并行算法

    增加了find, copy, sort的并行执行操作par并行, seq顺序, par_unseq并行非顺序

    std::vector v;
    std::find(std::execution::par, v.begin(), v.end(), 2);
    
  11. std::sample

    sample给定学列中的若干个元素,每个元素都有一个均等的被挑选的机会。

  12. std::clamp

    clamp的作用是获取一个在由高值、低值范围限定的给定值。

  13. std::reduce

    std::accmulate类似,在<numeric>中。

  14. prefix sum algorithms

    inclusive_scanexclusive_scan

  15. GCD (great common divisor)LCM (least common multiple)

    最大公约数和最小公倍数,最小公倍数是基于最大公约数进行计算的

    const int a = 9;
    const int b = 3;
    std::gcd(a, b); // 3
    std::lcm(a, b); // 
    
  16. std::not_fn

    返回给定函数结果的否定值

  17. 字符串转换to/from数字

    • to_chars()
    • from_chars()

标签:std,17,int,auto,c++,特性,C++,byte
From: https://www.cnblogs.com/solicit/p/18352806

相关文章

  • 【C++】马蹄集05 最大默契
    小码哥和小码妹是好朋友,他们有时会用一种方式检测双方的默契程度:两人分别给出一个字符串8和并进行若干次操作使s串变得和t串一样。操作分为两种:1.删除s串的第一个字符;2.将一个新字符插在s串的第一个字符之前。如果可以用正好n次操作使s串变为t串,就意味着他们两人很有......
  • C++特性
    C++特性C++主要版本:可以通过draft/papersatmain·cplusplus/draft(github.com)查看C++草案。C++98:C++的第一个国际标准ISO/IEC14882:1998,包括的特性有:模板、标准模板库、命名空间、异常处理等。C++03:2003年发布的版本ISO/IEC14882:2003,包括的特性有:内联函数、操作......
  • C/C++数字与字符串互相转换
    前言:在C/C++程序中,会需要把数字与字符串做出互相转换的操作,用于实现程序想要的效果。下面将介绍多种方法实现数字与字符串互相转换。字符串转为数字一、利用ASCII我们知道每个字符都有一个ASCII码,利用这一点可以将字符-'0'转为数字。在字母大小写转换时也可以利用这个性质......
  • 最大子矩阵(C/C++)
    简介:最大子矩阵问题是指在一个矩阵中找到一个子矩阵,使得该子矩阵的元素之和最大。解决该问题的常用方法是使用动态规划。先计算出每一行的前缀和,然后对于每一列的起始和终止位置,计算出该区域内每一行的和,得到一个一维数组。再对该一维数组使用动态规划求解最大子数组和的问题......
  • 【C++】protobuf的简单使用(通讯录例子)
    protobuf的简单使用(通讯录例子).proto文件的编写保留字段字段唯一编号protobuf的类型enum类型Any类型oneof类型map类型完整通讯录代码.proto文件write文件read文件运行结果.proto文件的编写syntax用于指定protobuf的语法;package当.proto文件编译后再*.pb.h文件中会......
  • LeetCode 算法:最小栈 c++
    原题链接......
  • [ARC179E] Rectangle Concatenation
    MyBlogs[ARC179E]RectangleConcatenation唐完了。稍微观察一下发现矩形只有两种形态。考虑暴力:从每个\(i\)开始向后扫,设\(f_{j,0}\)表示能否拼在左右,\(f_{j,1}\)表示能否拼在上下。设\(S_{l,r}\)表示\([l,r]\)内矩形的面积和,没想到用面积判就败了:\[\begin{aligned......
  • 探索-C--高级特性-全-
    探索C#高级特性(全)原文:ExploringAdvancedFeaturesinC#协议:CCBY-NC-SA4.0一、受关注的C#7C#7于2017年3月发布,是VisualStudio2017发布的一部分。如上所述。NETBlog中,C#7专注于数据消费、简化代码和提高性能。C#7最大的特点是元组和模式匹配。使用元......
  • c++中内存管理
    一、内存划分1、分区介绍(1)栈栈又称做堆栈,用于存储非静态局部变量、函数参数、返回值等,栈的空间是向下增长的。(2)内存映射段内存映射段是高效的I/O映射方式,用于装载一个共享的动态内存库。用户可使用系统接口创建共享共享内存,做进程间通信。(3)堆堆用于程序运行时动态内存分......
  • 暑假集训CSP提高模拟17
    \[暑假集训CSP提高模拟\operatorname{EIJ}_{2}(6)-1\]\(\operatorname{EIJ}_{k}(A)\)定义为有\(A\)个球,\(k\)个盒子,盒子相同,球不同,把全部球放入的方案数Hint易知\(\operatorname{EIJ}_k(A)=\dfrac{A^k}{k!}\),详见这篇文章其实我觉得构造的过程更有意思:对一个给定的正......