c++17 pmr

The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, with major new features being intro duced in 2011 and 2014. At press time, the C++17 standard is just around the corner. C++11 practically doubled the size of the standard library, adding such headers as , , and . C++17 doubles the library again, with additions such as , , and . A programmer who’s been spending time writing code instead of watching the standardization process might fairly feel that the standard library has gotten away fromhim--that there’s so many new things in the library that he'll never be able to master the whole thing, or even to sort the wheat fromthe chaff. After all, who wants to spend a month reading technical documentation on std::locale and std::ratio , just to find out that they aren't useful in your daily work? In this book, I'll teach you the most important features of the C++17 standard library. In the interest of brevity, I omit some parts, such as the aforementioned ; but we'll cover the entire modern STL (every standard container and every standard algorithm), plus such imp ortant topics as smart pointers, randomnumbers, regular expressions, and the new-in-C++17 library. I'll teach by example. You'll learn to build your own iterator type; your own memory allocator using std::pmr::memory_resource ; your own thread pool using std::future . I'll teach concepts beyond what you'd find in a reference manual. You'll learn the difference between monomorphic, polymorphic, and generic algorithms (Chapter 1 , Classical Polymorphism and Generic Programming ); what it means for std::string or std::any to be termed a "vocabulary type"(Chapter 5 , Vocabulary Types ); and what we might expect fromfuture C++ standards in 2020 and beyond. I assume that you are already reasonably familiar with the core language of C++11; for example, that you already understand how to write class and function templates, the difference between lvalue and rvalue references, and so on.
<think>好的,我现在需要回答用户关于C++17头文件的问题。用户之前已经询问过std::iterator被弃用的事情,现在转向头文件,可能想了解C++17新增或修改了哪些头文件,或者相关的变化。 首先,我需要回忆C++17标准中涉及头文件的部分。可能包括新增的头文件,比如<filesystem>,这是比较重要的新增库。还有<memory_resource>,这是针对多态内存资源的。此外,可能有某些现有头文件的更新,比如<optional>, <variant>, <any>,这些可能在C++17中引入。不过需要确认这些头文件是否确实是C++17新增的,比如<optional>确实是C++17的一部分。 然后,需要整理这些头文件的作用和主要类/函数。比如<filesystem>提供了文件系统的操作,包括路径、目录遍历等。<memory_resource>与内存池和分配器相关。还有可能其他头文件如<string_view>,虽然我好像记得<string_view>是C++17引入的,对应的头文件是<string_view>吗?是的,没错。 另外,C++17中可能有一些头文件的废弃或修改。比如之前提到的<ccomplex>, <ciso646>, <cstdalign>, <cstdbool>, <ctgmath>这些在C++17中被弃用,因为它们对应的C标准库部分在C++中用处不大或者有更好的替代。 还需要注意,有些头文件可能被合并或调整,比如<experimental>中的一些特性被正式纳入标准库,比如<experimental/filesystem>变成<filesystem>。这也是需要提到的点。 用户可能还关心如何正确使用这些新头文件,是否需要特定的编译器支持。比如,使用<filesystem>可能需要链接特定的库,比如在GCC中需要-lstdc++fs,Clang中可能需要-lc++fs,不过这个可能随着编译器版本不同而变化,需要指出可能需要查阅编译器的文档。 另外,要提到C++17中一些现有头文件的增强,比如<algorithm>中的新算法,如std::clamp, std::sample等,但用户的问题是头文件,所以可能重点在新增头文件而非现有头文件的新内容。不过可以简要提及这些内容,但需要明确区分。 可能的误区:用户可能混淆了C++17C++20的内容,比如<format>头文件是C++20的,所以需要确认哪些确实是C++17的。 总结需要分点,可能的结构是: 1. 新增头文件列表,每个头文件的作用和主要内容。 2. 被弃用或移除的头文件。 3. 现有头文件的重要更新(如果有)。 4. 使用这些头文件的注意事项,比如编译器支持、链接库等。 需要确保信息准确,比如确认每个头文件是否确实属于C++17。比如,<optional>, <variant>, <any>, <string_view>, <filesystem>, <memory_resource>都是C++17的新增头文件。 可能需要检查参考资料或权威来源,比如cppreference.com上的C++17页面,确认各个头文件的归属。 例如: - <optional>:提供std::optional - <variant>:提供std::variant和std::visit - <any>:提供std::any - <string_view>:提供std::string_view - <filesystem>:文件系统库 - <memory_resource>:内存池资源管理 - <charconv>:字符转换函数,如to_chars, from_chars 此外,C++17还引入了其他头文件,如<execution>用于并行算法,但该头文件属于标准算法的执行策略,属于现有头文件<algorithm>的扩展,但可能本身是新的头文件?或者执行策略是在<algorithm>中定义的?需要确认。根据记忆,执行策略相关的定义在<execution>头文件中,属于C++17的新增内容。 所以需要将<execution>列为新增头文件。 然后是被弃用的头文件,如前面提到的那些C兼容的头文件,如<ccomplex>等,这些在C++17中被标记为废弃。 在撰写回答时,需要结构清晰,分点说明,每个头文件简要介绍其用途和主要内容,并给出示例代码(如果需要)。同时,注意用户可能需要知道如何正确包含这些头文件,以及兼容性问题。 例如,对于<filesystem>,可能需要说明在使用时需要命名空间std::filesystem,以及如何操作路径等。 另外,要注意用户可能的实际应用场景,比如为什么需要这些新的头文件,能解决什么问题。例如,<filesystem>简化了文件操作,<optional>避免了使用裸指针或特殊值来表示可能缺失的值,提高代码安全性。 最后,提醒用户注意编译器的支持情况,比如C++17特性需要编译器支持相应的标准版本,如GCC 7及以上,Clang 5及以上,MSVC 2017等,并可能需要设置编译标志如-std=c++17。 现在需要将这些思路组织成中文回答,结构清晰,分点说明,确保准确性和实用性。</think>--- ### C++17 标准库新增头文件及关键变化 C++17 引入了多个新头文件,并对现有头文件进行了增强。以下是主要变化及新增头文件的详细介绍: --- ### 一、**新增头文件** #### 1. **`<filesystem>`** **作用**:提供跨平台文件系统操作接口。 **核心类/函数**: - `std::filesystem::path`:表示文件路径。 - `std::filesystem::directory_iterator`:遍历目录。 - `std::filesystem::create_directory`:创建目录。 - `std::filesystem::file_size`:获取文件大小。 **示例**: ```cpp #include <filesystem> namespace fs = std::filesystem; fs::path p = "data.txt"; if (fs::exists(p)) { std::cout << "文件大小: " << fs::file_size(p) << " 字节\n"; } ``` --- #### 2. **`<optional>`** **作用**:表示“可能有值”的容器,避免使用 `nullptr` 或特殊值标记无效状态。 **核心类**: - `std::optional<T>`:封装可能为空的 `T` 类型值。 **示例**: ```cpp #include <optional> std::optional<int> parse_int(const std::string& s) { try { return std::stoi(s); } catch (...) { return std::nullopt; // 表示无值 } } ``` --- #### 3. **`<variant>`** **作用**:实现类型安全的联合体(union),可存储多种类型的值。 **核心类/函数**: - `std::variant<Types...>`:可存储 `Types` 中的任意类型。 - `std::visit`:访问 `variant` 中的值。 **示例**: ```cpp #include <variant> #include <string> std::variant<int, std::string> data; data = 42; data = "Hello"; std::visit([](auto&& val) { std::cout << val << std::endl; }, data); ``` --- #### 4. **`<any>`** **作用**:存储任意类型的单值,支持类型安全的值提取。 **核心类**: - `std::any`:可存储任意类型的对象。 **示例**: ```cpp #include <any> std::any a = 42; a = std::string("Hello"); if (a.has_value() && a.type() == typeid(std::string)) { std::cout << std::any_cast<std::string>(a) << std::endl; } ``` --- #### 5. **`<string_view>`** **作用**:提供对字符串的非拥有(non-owning)视图,避免拷贝开销。 **核心类**: - `std::string_view`:轻量级字符串视图。 **示例**: ```cpp #include <string_view> void print(std::string_view sv) { std::cout << sv.substr(0, 5) << std::endl; // 无拷贝操作 } print("Hello, World!"); // 直接传递字面量 ``` --- #### 6. **`<memory_resource>`** **作用**:支持多态内存资源分配,用于实现自定义内存池。 **核心类**: - `std::pmr::memory_resource`:内存资源基类。 - `std::pmr::polymorphic_allocator`:多态分配器。 **示例**: ```cpp #include <memory_resource> #include <vector> std::pmr::monotonic_buffer_resource pool; std::pmr::vector<int> vec{&pool}; // 使用内存池分配元素 ``` --- #### 7. **`<charconv>`** **作用**:高性能的字符与数值转换。 **核心函数**: - `std::to_chars`:数值转字符串。 - `std::from_chars`:字符串转数值。 **示例**: ```cpp #include <charconv> char buffer[20]; int value = 42; auto [ptr, ec] = std::to_chars(buffer, buffer + 20, value); if (ec == std::errc{}) { std::cout << std::string_view(buffer, ptr - buffer); // 输出 "42" } ``` --- #### 8. **`<execution>`** **作用**:支持并行算法执行策略。 **核心内容**: - 执行策略:`std::execution::seq`(顺序)、`std::execution::par`(并行)、`std::execution::par_unseq`(并行+向量化)。 **示例**: ```cpp #include <algorithm> #include <execution> #include <vector> std::vector<int> data = {3, 1, 4, 1, 5}; std::sort(std::execution::par, data.begin(), data.end()); // 并行排序 ``` --- ### 二、**被弃用的头文件** 以下 C 标准库兼容头文件在 C++17 中被标记为弃用: - `<ccomplex>` - `<ciso646>` - `<cstdalign>` - `<cstdbool>` - `<ctgmath>` **原因**: - 这些头文件在 C++ 中用途有限(如 `<cstdbool>` 定义的 `bool` 类型已由 C++ 原生支持)。 - 建议直接使用 C++ 原生替代(如 `<complex>` 替代 `<ccomplex>`)。 --- ### 三、**现有头文件的重要增强** 1. **`<algorithm>`** - 新增算法:`std::clamp`(限制数值范围)、`std::sample`(采样元素)。 2. **`<numeric>`** - 新增:`std::gcd`(最大公约数)、`std::lcm`(最小公倍数)。 3. **`<iterator>`** - 弃用 `std::iterator`(用户应直接定义迭代器类型别名)。 --- ### 四、**使用注意事项** 1. **编译器支持**: - GCC 7+、Clang 5+、MSVC 2017 15.7+ 支持 C++17。 - 编译时需添加标志(如 `-std=c++17`)。 2. **文件系统库的链接**: - GCC/Clang 需链接 `-lstdc++fs` 或 `-lc++fs`(视编译器版本而定)。 3. **兼容性**: - 若需兼容旧代码,可通过 `#if __cplusplus >= 201703L` 条件编译。 --- ### 五、总结 C++17 新增的头文件(如 `<filesystem>`、`<optional>`)显著增强了标准库的功能,同时弃用了冗余的 C 兼容头文件。开发者应优先使用新特性以提升代码安全性和性能,并注意编译环境和迁移成本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值