c++_1st par

部署运行你感兴趣的模型镜像

学了好几天了,来总结一下学的东西。

最开始在看 thinking in c++ 英文原版。发现读得还挺顺的,没什么看不懂的地方。但是读了老久发现只学到了 cin cout.

因为就像做英语阅读那样的看,过一遍没有理解。。

跳过了preface 和introduction 直接就first c++ program了,感觉虚的很。

先是一个过渡的a tiny c-like library.愣是觉得讲的有点晦涩,好多都看不懂  。

算了 不要纯吐槽了。。。尴尬

哦 翻了一遍书 感觉什么都没学到。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

<think>我们按照C++标准的演进,分别介绍C++11C++14、C++17和C++20的主要特性及区别,并给出相应的代码示例。 注意:由于C++20的模块(Modules)和协程(Coroutines)等特性在编译器支持上可能还不完全,这里主要介绍标准特性,并给出在支持环境下的示例。</think># C++11C++14、C++17与C++20主要特性对比 下面我将详细解释各版本C++标准的主要特性差异,并提供代码示例说明关键变化。 ## 核心特性演进对比 | 特性类别 | C++11 (2011) | C++14 (2014) | C++17 (2017) | C++20 (2020) | |-----------------|----------------------------------|----------------------------------|----------------------------------|----------------------------------| | **类型推导** | `auto`、`decltype` | 泛型lambda、`decltype(auto)` | 结构化绑定、类模板参数推导 | Concepts约束、`auto`参数 | | **智能指针** | `shared_ptr`, `unique_ptr` | `make_unique` | - | `atomic<shared_ptr>` | | **函数增强** | Lambda表达式 | Lambda泛型捕获 | `constexpr` lambda | Lambda模板参数 | | **并发支持** | `thread`, `mutex`, `atomic` | `shared_timed_mutex` | `scoped_lock`, 并行算法 | `jthread`, `atomic_ref` | | **模板元编程** | 变参模板、类型别名 | 变量模板 | `if constexpr`、折叠表达式 | Concepts、Modules | | **容器/算法** | 无序容器、`array` | - | `optional`, `variant`, `any` | `span`, `ranges`库 | | **其他特性** | 移动语义、右值引用、nullptr | 二进制字面量、数字分隔符 | 内联变量、文件系统库 | 协程、三路比较运算符 | ## 详细特性对比与代码示例 ### 1. 类型系统增强 #### C++11: `auto` 和 `decltype` ```cpp // C++11 类型推导 auto num = 42; // int auto name = "Alice"s; // std::string std::vector<int> vec = {1, 2, 3}; for (auto it = vec.begin(); it != vec.end(); ++it) { // 使用迭代器 } // decltype 获取表达式类型 decltype(vec)::value_type x = 10; ``` #### C++14: `decltype(auto)` 和 泛型lambda ```cpp // C++14 decltype(auto) 完美转发返回类型 decltype(auto) get_value(int id) { return database.lookup(id); // 保留引用或值 } // 泛型lambda auto print = [](auto&& item) { std::cout << item << "\n"; }; print(42); // int print("Hello"); // const char* ``` #### C++17: 结构化绑定和类模板参数推导 ```cpp // C++17 结构化绑定 std::map<int, std::string> data = {{1, "one"}, {2, "two"}}; for (const auto& [key, value] : data) { // 解构键值对 std::cout << key << ": " << value << "\n"; } // 类模板参数推导 std::vector nums = {1, 2, 3}; // 无需指定<int> std::mutex mtx; std::lock_guard lock(mtx); // 无需指定<std::mutex> ``` #### C++20: Concepts约束 ```cpp // C++20 概念约束 template <typename T> concept Addable = requires(T a, T b) { { a + b } -> std::same_as<T>; }; template <Addable T> T sum(T a, T b) { return a + b; } auto result = sum(3.14, 2.71); // 满足Addable // auto err = sum("a", "b"); // 编译错误:不满足概念 ``` ### 2. Lambda表达式演进 #### C++11: 基本Lambda ```cpp // C++11 Lambda基础 std::vector<int> nums = {1, 2, 3}; std::sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; }); // 降序排序 ``` #### C++14: 泛型Lambda和捕获初始化 ```cpp // C++14 泛型Lambda和捕获初始化 auto factory = [count = 0]() mutable { return count++; }; std::cout << factory() << ", "; // 0 std::cout << factory() << "\n"; // 1 ``` #### C++17: `constexpr` Lambda ```cpp // C++17 constexpr Lambda constexpr auto square = [](int n) { return n * n; }; static_assert(square(5) == 25); // 编译时计算 ``` #### C++20: Lambda模板参数 ```cpp // C++20 Lambda模板参数 auto print = []<typename T>(T value) { std::cout << value << "\n"; }; print(42); // int print(3.14); // double ``` ### 3. 智能指针演进 #### C++11: `shared_ptr` 和 `unique_ptr` ```cpp // C++11 智能指针基础 std::shared_ptr<int> p1 = std::make_shared<int>(42); std::unique_ptr<int> p2 = std::make_unique<int>(99); // C++14才标准化 ``` #### C++14: `make_unique` ```cpp // C++14 make_unique标准化 auto uptr = std::make_unique<std::string>("C++14"); ``` #### C++20: 原子智能指针 ```cpp // C++20 原子智能指针 std::atomic<std::shared_ptr<int>> atomicPtr; // 线程安全操作 std::thread t1([&]() { atomicPtr.store(std::make_shared<int>(100)); }); std::thread t2([&]() { if (auto ptr = atomicPtr.load()) { std::cout << "Value: " << *ptr << "\n"; } }); ``` ### 4. 并发编程演进 #### C++11: 基础线程支持 ```cpp // C++11 线程基础 std::mutex mtx; std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) { threads.emplace_back([i, &mtx]() { std::lock_guard<std::mutex> lock(mtx); std::cout << "Thread " << i << "\n"; }); } ``` #### C++14: `shared_timed_mutex` ```cpp // C++14 共享互斥量 std::shared_timed_mutex rw_mutex; // 读操作(共享锁) auto read = [&]() { std::shared_lock<std::shared_timed_mutex> lock(rw_mutex); // 读取共享数据 }; // 写操作(独占锁) auto write = [&]() { std::unique_lock<std::shared_timed_mutex> lock(rw_mutex); // 修改共享数据 }; ``` #### C++17: `scoped_lock` 和并行算法 ```cpp // C++17 scoped_lock(多互斥量死锁避免) std::mutex mtx1, mtx2; auto safe_access = [&]() { std::scoped_lock lock(mtx1, mtx2); // 同时锁定多个互斥量 // 安全访问共享资源 }; // 并行算法 std::vector<int> data(1000); std::for_each(std::execution::par, data.begin(), data.end(), [](int& n) { n *= 2; }); // 并行执行 ``` #### C++20: `jthread` 和 `atomic_ref` ```cpp // C++20 jthread(自动join) std::jthread worker([](std::stop_token st) { while (!st.stop_requested()) { std::cout << "Working...\n"; std::this_thread::sleep_for(1s); } }); // 主线程退出时自动join并停止工作线程 // atomic_ref(原子引用) int counter = 0; std::vector<std::jthread> threads; for (int i = 0; i < 10; ++i) { threads.emplace_back([&]() { std::atomic_ref<int> atomicCounter(counter); for (int j = 0; j < 1000; ++j) { atomicCounter++; } }); } // 所有线程结束后 std::cout << "Final counter: " << counter << "\n"; // 10000 ``` ### 5. 元编程与模板演进 #### C++11: 变参模板 ```cpp // C++11 变参模板 void print() {} template <typename T, typename... Args> void print(T first, Args... args) { std::cout << first << " "; print(args...); } print(1, 3.14, "hello"); // 输出: 1 3.14 hello ``` #### C++14: 变量模板 ```cpp // C++14 变量模板 template <typename T> constexpr T pi = T(3.1415926535897932385L); auto area = pi<double> * r * r; ``` #### C++17: `if constexpr` 和折叠表达式 ```cpp // C++17 if constexpr 和折叠表达式 template <typename... Args> auto sum(Args... args) { if constexpr (sizeof...(args) == 0) { return 0; } else { return (args + ...); // 折叠表达式 } } auto s1 = sum(1, 2, 3); // 6 auto s2 = sum(); // 0 ``` #### C++20: Concepts 和 Modules ```cpp // C++20 Modules (示例) // math.cppm export module math; export template <typename T> T add(T a, T b) { return a + b; } // main.cpp import math; int main() { auto result = add(3, 4); // 7 return 0; } ``` ## 迁移建议 ### 何时使用哪个版本 1. **C++11**:基础现代化特性,广泛支持 2. **C++14**:对C++11的小幅改进,推荐作为最低要求 3. **C++17**:显著增强标准库,简化代码 4. **C++20**:重大变革,适合新项目开发 ### 迁移注意事项 ```cpp // 条件编译示例 #if __cplusplus >= 202002L // C++20代码 #include <ranges> using namespace std::ranges; #elif __cplusplus >= 201703L // C++17代码 #include <filesystem> namespace fs = std::filesystem; #elif __cplusplus >= 201402L // C++14代码 #include <memory> using std::make_unique; #else // C++11回退 #include <memory> template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } #endif ``` ## 编译器支持情况 | 编译器 | C++11 | C++14 | C++17 | C++20 | |-------------|-------|-------|-------|-------| | GCC | 4.8+ | 5.0+ | 7.0+ | 10.0+ | | Clang | 3.3+ | 3.4+ | 5.0+ | 10.0+ | | MSVC | 2015+ | 2017+ | 2017+ | 2019+ |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值