随笔分类 - C++代码片段
摘要:仿函数:先了解一下为什么需要“仿函数” 考虑将函数指针做为函数 A 的形参的情况下,假如需求变更,需要修改函数指针的签名。这种情况下,在修改函数指针声明之后,还必须修改函数 A。 “仿函数”的存在目的,就是我们希望不修改函数 A,仍然可以实现函数签名的变更。 C++ 仿函数_恋喵大鲤鱼的博客-CSD
阅读全文
摘要:内存映射可以加速磁盘文件的读取速度。 简单说明原理:将磁盘的一段空间映射到内存的地址中,和CPU的交互过程中,减少了缓存申请、缓存释放等过程,所以速度更快。 详细的解释: 一文搞懂内存映射(Memory Map)原理 - 知乎 (zhihu.com) 内存映射的实例代码(Windows) #incl
阅读全文
摘要:使用标准库 #include <chrono> int main() { auto stampBeg = std::chrono::steady_clock::now(); // do someting Sleep(2400); auto stampEnd = std::chrono::steady
阅读全文
摘要:摘录自:https://blog.csdn.net/to_baidu/article/details/53763683 Exception::~Exception() throw() {} 表示此函数不会抛出任何异常。 throw() 修饰,表示“限制函数抛出任何异常” throw(...) 修饰,
阅读全文
摘要:#include <iostream> #include <string> #include <stack> // https://zh.cppreference.com/w/cpp/container/stack // std::stack 类是容器适配器,它给予程序员栈的功能——特别是 FILO (先进后出)数据结构。 // 该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶
阅读全文
摘要:#include <iostream> #include <string> #include <deque> // https://zh.cppreference.com/w/cpp/container/deque // vector 和 deque 的差别 // https://www.cnblogs.com/zhuyf87/archive/2012/12/09/2809896.html usi
阅读全文
摘要:主要差别: list 是双向链表,forward_list 是双向链表。 成员函数差异: * forward_list 中设计的一系列 xxx_after() 的原因: 其中的元素仅知道后面的元素,不知道前面的元素。(单向链表的特性)所以类似于 insert 这样的操作,需要指定前一个元素的迭代器,
阅读全文
摘要:#include <iostream> #include <string> #include <forward_list> using namespace std; // https://zh.cppreference.com/w/cpp/container/forward_list std::ostream& operator<<(std::ostream& ostr, const std::f
阅读全文
摘要:* std::list 是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代但在空间上效率稍低。
阅读全文
摘要:#include #include #include using namespace std; // https://zh.cppreference.com/w/cpp/container/array int main() { ///array arr({ 1,2,3 }); // 非法 array arr1{ { 1,2,3 } }; // 不可以扩容,属于固定大小的数组。...
阅读全文
摘要:#include #include using namespace std; int main() { int ar[10] = { 1,2,3,4,5,6,7,8,9,0 }; std::vector vec5(ar, ar + 10); // reverse size_t cap1 = vec5.capacity(); // = 10 vec5.reserve(20)...
阅读全文
摘要:#include <iostream> #include <vector> #include <chrono> #include <windows.h> using namespace std; using namespace std::chrono; // 要考虑 copy ctor 和 copy
阅读全文
摘要:#include <iostream> #include <vector> using namespace std; int main() { // 初始化的方式 std::vector<int> vec1; //std::vector<int> vec2(1750000);// 7G std::v
阅读全文
摘要:#include // operator Type() 类型操作符重载 // operator int() // operator double() // ... ////////////////////////////////////////////////////////// class Rectangle { public: #ifdef Want_compiler_to_add_...
阅读全文
摘要:#include // operator Type() 类型操作符重载 // operator int() // operator double() // ... ////////////////////////////////////////////////////////// class Rectangle { public: Rectangle(const int w, cons...
阅读全文
摘要:#include <iostream> // 可变模板参数 // 此例:可以构造可变数量,可变类型的函数输入。 // 摘自:https://www.cnblogs.com/qicosmos/p/4325949.html ///////////////////////////// using namespace std; //递归终止函数 void print() { cout << "empty"
阅读全文
摘要:#include // 可变模板参数 // 此例:可以构造可变数量,可变类型的函数输入。 // 摘自:https://www.cnblogs.com/qicosmos/p/4325949.html ///////////////////////////// using namespace std; //递归终止函数 void print() { cout void print(T h...
阅读全文
摘要:#include <iostream> // overloading "operator >> " outside class // >> 应该定义在类之外。 ////////////////////////////////////////////////////////// class Rectangle { public: Rectangle(int w, int h) : width(w),
阅读全文
摘要:虚函数的几点说明: 1. 当一个成员函数定义为虚函数时,其派生类中的同名函数也自动为虚函数。无论其是否添加了 virtual 关键字。 为了能良好的阅读代码,请加上。 2. 父类的虚函数,就是为了让子类中的同名成员函数覆盖。这样,父类对象的指针就可以指向子类对象,并调用子类的同名函数。 3. 纯虚函
阅读全文
摘要:Stack 堆 存在于某作用域内的一块空间。说白了就是函数产生的空间,用于存放函数的变量、返回地址。 在函数体中声明的局部变量,就时存储在Stack中。 Heap 栈 由操作系统提供的全局空间。在程序结束后由系统释放。 定义的全局变量就在这个内存区域中。 静态数据区 static对象所在区域。 文字
阅读全文