网站空间代理站,建筑工程网络图中查找线路,济南口碑最好的装修公司,seo诊断晨阳✍个人博客#xff1a;https://blog.csdn.net/Newin2020?spm1011.2415.3001.5343 #x1f4da;专栏地址#xff1a;C/C知识点 #x1f4e3;专栏定位#xff1a;整理一下 C 相关的知识点#xff0c;供大家学习参考~ ❤️如果有收获的话#xff0c;欢迎点赞#x1f44d;… ✍个人博客https://blog.csdn.net/Newin2020?spm1011.2415.3001.5343 专栏地址C/C知识点 专栏定位整理一下 C 相关的知识点供大家学习参考~ ❤️如果有收获的话欢迎点赞收藏您的支持就是我创作的最大动力 唠叨唠叨在这个专栏里我会整理一些琐碎的 C 知识点方便大家作为字典查询~ C20 新特性
模块
从 C 语言中C 继承了 #include 机制依赖从头文件使用文本形式包含 C 源代码这些头文件中包含了接口的文本定义。一个流行的头文件可以在大型程序的各个单独编译的部分中被 #include 数百次。基本问题是
(1) 不够卫生一个头文件中的代码可能会影响同一翻译单元中包含的另一个 #include 中的代码的含义因此 #include 并非顺序无关。宏是这里的一个主要问题尽管不是唯一的问题。
(2) 分离编译的不一致性两个翻译单元中同一实体的声明可能不一致但并非所有此类错误都被编译器或链接器捕获。
(3) 编译次数过多从源代码文本编译接口比较慢。从源代码文本反复地编译同一份接口非常慢。
所以在 C 程序中改进模块化是一个迫切的需求。
#include iostream
int main() {std::cout Hello, C! std::endl;
}这段标准代码有 70 个左右的字符但是在 #include 之后它会产生 419909 个字符需要编译器来消化。尽管现代 C 编译器已有傲人的处理速度但模块化问题已经迫在眉睫。
模块化是什么意思顺序独立性import X; import Y; 应该与 import Y; import X; 相同。
换句话说任何东西都不能隐式地从一个模块“泄漏”到另一个模块。
这是 #include 文件的一个关键问题。
#include 中的任何内容都会影响所有后续的 #include。
顺序独立性是“代码卫生”和性能的关键。
C 20 中正式引入了模块的概念模块是一个用于在翻译单元间分享声明和定义的语言特性。它们可以在某些地方替代使用头文件。
其主要优点如下 没有头文件。 声明实现仍然可分离但非必要。 可以显式指定导出哪些类或函数。 不需要头文件重复引入宏include guards。 模块之间名称可以相同并且不会冲突。 模块只处理一次编译更快头文件每次引入都需要处理需要通过 pragma once 约束。 预处理宏只在模块内有效。 模块的引入与引入顺序无关。
创建模块
源文件-添加-新建项-Module
创建 ***.ixx 文件
//创建模块
// mymodule.ixx //模块名和文件名没有强制要求一般会相同
export module helloworld; //模块声明
import iostream; //导入声明 注意号
export void hello() { //导出声明std::cout Hello world!\n;
}//导入模块
//main.cpp
import helloworld; //导入声明
int main() {hello();
}协程
协程就是一个可以挂起suspend和恢复resume的函数不能是 main 函数。你可以暂停协程的执行 去做其他事情然后在适当的时候恢复到暂停的位置继续执行。协程让我们使用同步方式写异步代码。
C 提供了三个方法挂起协程co_await co_yield 和 co_return。
C20 协程只是提供协程机制而不是提供协程库。C20 的协程是无栈协程无栈协程是一个可以挂起/恢复的特殊函数是函数调用的泛化且只能被线程调用本身并不抢占内核调度。
C20 提供了三个新关键字(co_await、co_yield 和 co_return)如果一个函数中存在这三个关键字之一那么它就是一个协程。
co_yield some_value保存当前协程的执行状态并挂起返回 some_value 给调用者。
co_await some_awaitable如果 some_awaitable 没有 ready就保存当前协程的执行状态并挂起。
co_return some_value彻底结束当前协程返回 some_value 给协程调用者。 三向比较运算符
也叫三路比较运算符三路比较结果如下
(a b) 0 // 如果 a b则为 true(a b) 0 // 如果 a b则为 true(a b) 0 // 如果 a 与 b 相等或者等价则为 true
类似于 C 的 strcmp 函数返回 -1, 0, 1。
一般情况自动生成所有的比较操作符如果对象是结构体则逐个比较可以用下面代码代替所有的比较运算符
auto X::operator(const Y) default;高级情况指定返回类型(支持 6 种所有的比较运算符)
int num1 100, num2 100;
if ((num1 num2) 0) {cout num1 num2 endl;
}
else if ((num1 num2) 0) {cout num1 num2 endl;
}
else {cout num1 num2 endl;
}范围 ranges
范围库始于 Eric Niebler 对 STL 序列观念的推广和现代化的工作。它提供了更易于使用、更通用及性能更好的标准库算法。
例如C20 标准库为整个容器的操作提供了期待已久的更简单的表示方法。
void func1(vectorstring s) {sort(s); //而不是 sort(vs.begin(), vs.end());
}举个例子
#include vector
#include ranges
#include iostream
using namespace std;
int main()
{auto ints views::iota(0, 10);//生成0-9auto even [](int i) { return 0 i % 2; };auto square [](int i) { return i * i; };for (int i : ints | views::filter(even) | views::transform(square))cout i ;return 0;
}日期和时区
日期库是日期库是多年工作和实际使用的结果,它基于 chrono 标准库的时间支持。在 2018 年它进入了 C20并和旧的时间工具一起放在中。
#include iostream
#include chrono
using namespace std;
using namespace std::chrono;
int main()
{//creating a year auto y1 year{ 2019 };auto y2 2019y;//creating a mouthauto m1 month{ 9 };auto m2 September;//creating a day auto d1 day{ 18 };auto d2 18d;year_month_day date1{ 2022y,July, 21d };auto date2 2022y / July / 21d;chrono::year_month_day date3{ Monday[3] / July / 2022 };cout date1 endl;cout date2 endl;cout date3 endl;return 0;
}格式化
iostream 库提供了类型安全的 I/O 的扩展但是它的格式化工具比较弱。
另外还有的人不喜欢使用 分隔输出值的方式。
格式化库提供了一种类 printf 的方式去组装字符串和格式化输出值同时这种方法类型安全、快捷并能和 iostream 协同工作。
类型中带有 运算符的可以在一个格式化的字符串中输出。
string s1 C;
cout format(The string {} has {} characters, s1, s1.size());
cout format(The string {0} has {1} characters, s1, s1.size()) endl;
cout format(The string {1} has {0} characters, s1.size(), s1) endl;跨度
越界访问有时也称为缓冲区溢出从 C 的时代以来就一直是一个 严重的问题。考虑下面的例子
void func1(int* p, int n) { //n是什么for (int i 0; i n; i) {p[i] 7; //是否可行}
}span 类模板就这样被放到 C 核心指南的支持库中。
void func(spanint a) { //span包含一个指针和一条大小信息for (int x : a) {x 7; //可以}
}范围 for 从跨度中提取范围并准确地遍历正确数量的元素无需代价高昂的范围检查。这个例子说明了一个适当的抽象可以同时简化写法并提升性能。对于算法来说相较于挨个检查每一个访问的元素明确地使用一个范围比如 span要容易得多开销也更低。
并发
std::promise
std::promise 和 std::future 是一对通过它们可以进行更加灵活的任务控制。
promise 通过函数 set_value() 传入一个值异常或者通知并异步的获取结果。
#include iostream
#include future
#include format
using namespace std;
void product(promiseint intPromise, int v1, int v2) {intPromise.set_value(v1 * v2);
}
int main() {int num1 200;int num2 300;promiseint productPromise;futureint productResult productPromise.get_future();jthread productThread(product, move(productPromise), num1, num2);cout format(product is {}\n, productResult.get());
}std::future
从 promise 获取值询问值是否可用等待通知创建 shared_future std::future_status
调用后 wait_for 或者 wait_until 返回的结果。
enum class future_status
{ready, //成功timeout, //超时deferred //延迟
};案例
#include iostream
#include future
#include format
using namespace std;
void getAnswer(promiseint intPromise)
{this_thread::sleep_for(2s);intPromise.set_value(100);
}
int main()
{promiseint answerPromise;auto fut answerPromise.get_future();jthread productThread(getAnswer, move(answerPromise));future_status status{};do{status fut.wait_for(0.5s);cout 结果未准备完成 endl;} while (status ! future_status::ready);cout format(answer is {}\n , fut.get());
}