随笔分类 - 【02】C++
摘要:互斥锁 相比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
阅读全文
摘要:遍历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
阅读全文
摘要:string流输入输出 #include <iostream> #include <streambuf> #include <fstream> #include <vector> #include <string> #include <cstring> #include <memory> // 自定
阅读全文
摘要:std::forward 完美转发 1. std::forward代码原型: template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); }
阅读全文
摘要:内存分配方式 一、分配方式简介 在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。 栈:在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。 堆:就
阅读全文
摘要:Part II:vmware虚拟机安装centos7操作系统教程 参考资料
阅读全文
摘要:继承的构造函数 子类为完成基类初始化,在C++11之前,需要在初始化列表调用基类的构造函数,从而完成构造函数的传递。如果基类拥有多个构造函数,那么子类也需要实现多个与基类构造函数对应的构造函数。 struct Base { Base(int x) { std::cout << "Base(" <<
阅读全文
摘要:protected 和 private修饰的构造函数:连接 1. 在类的外部创建对象时,不能调用protected或private修饰的构造函数。 2.当子类中的构造函数调用父类的private构造函数时会错,当子类中的构造函数调用父类中的 public或protected构造函数时是对的 1 #i
阅读全文

浙公网安备 33010602011771号