C++ 现代教程三

发布时间:2026/11/2 6:10:08

C++ 现代教程三 // 模板参数类型区分 template class T static std::string cppdemangle() { std::string s{cppdemangle(typeid(std::remove_cv_tstd::remove_reference_tT))}; if (std::is_const_vstd::remove_reference_tT) s const; if (std::is_volatile_vstd::remove_reference_tT) s volatile; if (std::is_lvalue_reference_vT) s ; if (std::is_rvalue_reference_vT) s ; return s; } // 显示转换枚举值 template class T, std::enable_if_tstd::is_enum_vT, int 0 constexpr std::underlying_type_tT to_underlying(T t) noexcept { return static_caststd::underlying_type_tT(t); } template class T, std::enable_if_t!std::is_enum_vT, int 0 constexpr T to_underlying(T t) noexcept { return t; } //文件操作 static std::string file_get_content(std::string const path) { std::ifstream fin(path); std::string content; std::istreambuf_iteratorchar iit(fin), eiit; std::back_insert_iteratorstd::string sit(content); std::copy(iit, eiit, sit); return content; } static void file_put_content(std::string const path, std::string const content) { std::ofstream fout(path); fout content; } static bool file_exists(std::string const path) { std::ifstream fin(path); return (bool)fin; } // win min max冲突 #ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX #endif #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES #endif /* FUN FACT: DID YOU KNOW THAT MICROSOFT BUYS GITHUB? */ #if __has_include(windows.h) #include windows.h #elif __has_include(math.h) #include math.h #endif /* FUN FACT: DID YOU KNOW THAT MICROSOFT INVENTS VSCODE? */ #ifdef min #undef min #endif #ifdef max #undef max #endif #ifdef near #undef near #endif #ifdef far #undef far #endif #endif // 耗时 #define TICK(x) auto bench_##x std::chrono::steady_clock::now(); #define TOCK(x) std::printf(%s: %lfs\n, #x, std::chrono::duration_caststd::chrono::durationdouble(std::chrono::steady_clock::now() - bench_##x).count()); // 随机字符串生成 inline std::string randomStringGenerator(const int len) { std::string res; static const char alphanum[] 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz; for (int i 0; i len; i) res alphanum[((size_t)std::rand() % (sizeof(alphanum) - 1))]; return res; } // C20之三目运算 templatetypename T void compare(T x, T y) { auto ret x y; std::cout name: typeid(ret).name() std::endl; if (ret 0) std::cout x y: x , y std::endl; else if (ret 0) std::cout x y: x , y std::endl; else std::cout x y: x , y std::endl; } // C20之字符与数值转换 #include charconv // std::to_chars: converts an integer or floating-point value to a character sequence std::arraychar, 10 buffer; // 注意:std::errc没有到bool的隐式转换,所以你不能像下面这样检查: if (res.ec) {} or if (!res.ec) {} if (auto [ptr, ec] std::to_chars(buffer.data(), buffer.data() buffer.size(), val_int); ec std::errc()) { std::cout str: std::string_view(buffer.data(), ptr - buffer.data()) \n; // str:88 *ptr \0; // 保证结尾有一个空字符 std::cout buffer: buffer.data() \n; // buffer:88 } else { std::cerr fail to call to_char: std::make_error_code(ec).message() \n; return -1; } auto str2 456.789; float val2{ 0.f }; auto [ptr2, ec2] std::from_chars(str2.data(), str2.data() str2.size(), val2); if (ec2 std::errc()) std::cout val2: val2 \n; // val2:456.789 else if (ec2 std::errc::invalid_argument) std::cerr Error: this is not a number: str2 \n; else if (ec2 std::errc::result_out_of_range) std::cerr Error: this number is larger than an float: str2 \n; else std::cerr fail to call from_chars: std::make_error_code(ec2).message() \n; // C17之并行策略 #include algorithm #include iostream #include vector #include execution std::vectorint v { 5, 2, 3, 1, 4 }; std::vectorint v2(5); std::sort(std::execution::seq, v.begin(), v.end()); // 向量 std::for_each(std::execution::unseq, v.begin(), v.end(), [](int x) { std::cout x ; }); // 向量化单线程 std::transform(std::execution::par, v1.begin(), v1.end(), v2.begin(), [](int x) { return x * x; }); // 多线程 std::for_each(std::execution::par_unseq, v.begin(), v.end(), [](int x) { std::cout x ; }); // 硬软件或SMID https://www.geeksforgeeks.org/execution-policy-of-stl-algorithms-in-modern-cpp/ // C头文件定义全局变量 class InlineVariable { public: inline static int var{ 10 }; // 不带inline,则只能在类外初始化;即使在类外初始化,如果被多个cpp文件包含也会error;带inline后,即使被多个cpp文件包含也OK inline static std::string name{ Messy_Test }; // 整个程序中只有一个 inline static thread_local int count{ 1 }; // 每个线程有一个 std::string city{ BeiJing }; // 每个实例有一个 }; inline InlineVariable inline_variable; // 不带inline,则会报重复定义;带inline后,即使被多个cpp文件包含也OK // 获取实际对象地址 std::addressof(c)C11中包含了以下几种String literal : Ordinary string literalL : Wide string literalu8 : UTF-8 string literalu : UTF-16 string literalU : UTF-32 string literalR : Raw string literalC 现代教程二-CSDN博客创作不易小小的支持一下吧
延伸阅读

更多相关文章

2026/11/2 6:05:08

FPGA+STM32相控阵雷达原型:PLFM_RADAR系统设计与实现

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 6:05:08

扫地机器人回充座通信报文解析与回充定位全链路实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 6:05:08

工业SPI隔离通信设计:容性隔离器与MCU协同实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 7:25:15

6S大气校正手册精读:辐射传输模型原理与工程实践指南

简介:这份PDF文档是6S模型(第二代太阳光谱卫星信号模拟)的用户操作说明,面向从事定量遥感、大气校正研究的科研人员与工程技术人员,帮助其理解大气传输过程中吸收与散射效应对卫星观测信号的影响。文档系统阐述了大气吸…

2026/11/2 7:25:15

SSH客户端安装配置全攻略:从下载甄别到安全使用

SSH 客户端这东西,但凡摸过几天服务器的人都不陌生。早些年大家伙儿手里攥着的工具五花八门,有人用命令行原生的 ssh,有人用带图形界面的终端模拟器,还有一批人专门找那种"一站式"的 Windows 桌面客户端,图的…

2026/11/2 7:25:15

6S大气传输模型:从吸收散射到遥感校正的工程实践

简介:这份PDF文档是6S大气辐射传输模型的官方用户指南(Version 2),面向从事定量遥感、大气校正与卫星数据处理的科研人员和工程师,用于理解并模拟太阳—地表—传感器路径上的大气影响。文档系统讲解大气吸收与散射效应…

2026/11/2 7:25:15

使用NVIDIA Isaac Sim 4.5进行四足机器人强化学习训练

1. 项目概述四足机器人强化学习训练是当前机器人领域的热门研究方向,而NVIDIA Isaac Sim 4.5作为强大的机器人仿真平台,为这一研究提供了理想的开发环境。本教程将详细讲解如何利用Isaac Sim 4.5完成四足机器人从零开始的强化学习训练全流程。在实际机器…

2026/11/2 7:25:15

Prometheus在Ubuntu 22.04上的部署与优化实践

1. 运维监控系统选型与Prometheus核心优势在当今复杂的IT基础设施环境中,监控系统已经成为运维工作的神经中枢。作为一名有十年运维经验的工程师,我见证过从Nagios到Zabbix再到Prometheus的技术演进历程。为什么Prometheus能在短短几年内成为云原生时代的…

2026/11/2 7:20:15

金融科技测试实战:区块链监管系统测试框架构建

1. 项目背景与核心挑战作为一名在金融科技领域深耕多年的软件测试工程师,我最近参与了一个极具挑战性的项目——为虚拟经济监管系统构建数字化测试框架。这个项目让我深刻体会到,在区块链和数字资产快速发展的今天,传统的测试方法已经无法满足…

2026/11/2 6:08:01

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/2 6:39:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 1:04:04

工业级SPI隔离设计:硬件抗扰与信号完整性实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 1:04:04

PLFM_RADAR:主动探测数据通道,构建低误报的数据感知系统

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 1:04:04

工业SPI隔离设计:MAX14431与STM32F746硬件抗干扰实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/2 6:08:01

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/2 6:39:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑