随笔分类 - 【01】C++
摘要:__VA_ARGS__和__VA_OPT__ 1. #用来把参数转换成字符串: #include <stdio.h> #define P(A) printf("%s: %d\n", #A, A); int main(int argc, char **argv) { int a = 1, b = 2;
阅读全文
摘要:stream使用xshell连接报错 Xshell连接时提示:SSH服务拒绝了密码。请再试一次。 解决方法: vim /etc/ssh/sshd_config PermitRootLogin yes yum 安装报错: # yum install yum-utils Loaded plugins:
阅读全文
摘要:priority_queue 优先队列 大根堆(降序) 构造一个空的优先队列(此队列默认为大根堆) priority_queue<int> big_heap; 另一种构造大根堆的方法: priority_queue<int, vector<int>, less<int>> big_heap; 小根堆
阅读全文
摘要:调用基类的虚函数 代码实现: #include <iostream> class Base { public: virtual void f() { std::cout << "Base::base()" << std::endl; } }; class Test : public Base { p
阅读全文
摘要:centos8 stream仓库配置 默认仓库: [root@centos8-stream yum.repos.d]# ll total 48 -rw-r--r--. 1 root root 713 Mar 28 2022 CentOS-Stream-AppStream.repo -rw-r--r-
阅读全文
摘要:互斥锁 相比std::lock_guard的优势: 功能 std::lock_guard std::unique_lock 自动加锁+自动解锁 支持 支持 手动加锁/解锁 不支持 支持 延迟加锁(defer_lock) 不支持 支持 条件变量wait支持 不支持 支持 可移动(不可复制) 不可移动
阅读全文
摘要:pair 1. 示例 #include <iostream> #include <utility> int main() { auto p = std::make_pair(1, 3.14); std::cout << '(' << std::get<0>(p) << ", " << std::ge
阅读全文
摘要:std::string_view std::string_view 类原型: template<class CharT, class Traits = std::char_traits<CharT>> class basic_string_view; 构造函数 1. 构造函数原型: constexp
阅读全文
摘要:std::begin和std::end 1. std::begin、std::end函数的用法 #include <iostream> #include <vector> using namespace std; int main() { std::vector<int> v{1, 2, 3, 4,
阅读全文
摘要:折叠表达式 表格4.1列出所有可能的表达式: #include <iostream> #include <string> template <typename ... Ts> auto sum(Ts ... ts) { return (... + ts); } int main() { std::c
阅读全文
摘要:sizeof : 获取内存存储的大小。alignof : 获取地址对其的大小,POD里面最大的内存对其的大小。 1 struct A{ //non-POD type 2 int avg; 3 int avg2; 4 double c; 5 A(int a,int b):avg((a+b)/2){ 6
阅读全文
摘要:智能指针shared_ptr与unique_ptr unique_ptr支持动态数组,而shared_ptr不能直接支持动态数组: std::unique_ptr<int []> ptr(new int[10]); //合法, std::shared_ptr<int []> ptr(new int[
阅读全文
摘要:https://zhuanlan.zhihu.com/p/110079635 https://zhuanlan.zhihu.com/p/42838850https://www.jianshu.com/p/6960811ac89chttps://www.cnblogs.com/shuimuzhushu
阅读全文
摘要:编写重载模板 #include <iostream> #include <sstream> #include <string> using namespace std; template <typename T> string debug_rep(const T &t) { ostringstrea
阅读全文
摘要:宏 1. #用来把参数转换成字符串: #include <stdio.h> #define P(A) printf("%s: %d\n", #A, A); int main(int argc, char **argv) { int a = 1, b = 2; P(a); P(b); P(a+b);
阅读全文
摘要:ISO C++ 标准展示了作为第三个参数传递给 std::sort() 函数的简单 lambda: 1 #include <algorithm> 2 #include <cmath> 3 4 void abssort(float* x, unsigned n) { 5 std::sort(x, x
阅读全文
摘要:位域 基础语法: struct 结构体名 { 数据类型 成员名 : 位数; 数据类型 成员名 : 位数; }; 示例: #include <iostream> struct Flags { unsigned int a : 1; // 占1位 unsigned int b : 2; // 占2位 u
阅读全文
摘要:遍历std::stuple 子类为完成基类初始化,在C++11之前,需要在初始化列表调用基类的构造函数,从而完成构造函数的传递。如果基类拥有多个构造函数,那么子类也需要实现多个与基类构造函数对应的构造函数。 1 class Base 2 { 3 public: 4 Base(int va) : m_
阅读全文
摘要:重载运算符 重载调用调用运算符 #include <iostream> struct absInt { int operator() (int val) const { return val < 0 ? -val : val; } }; int main() { int i = -42; absIn
阅读全文