随笔分类 -  C/C++

摘要:1. 必须使用初始化列表初始化一个引用成员; class CBase { public: CBase(int a) : m_a(a) //正确,m_a是一个变量的引用,必须通过初始化列表初始化 { // m_a = a; //错误 cout << "m_a:" << m_a << endl; } p 阅读全文
posted @ 2021-03-30 19:24 唯一诺 阅读(1857) 评论(0) 推荐(0)
摘要:https://chenqx.github.io/2014/09/25/Cpp-Memory-Management/ 阅读全文
posted @ 2021-03-29 08:54 唯一诺 阅读(49) 评论(0) 推荐(0)
摘要:1. static_cast用法:static_cast < type-id > ( exdivssion ) 该运算符把exdivssion转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:① 用于类层次结构中基类和子类之间指针或引用的转换。 进行上行转换(把 阅读全文
posted @ 2021-03-27 10:54 唯一诺 阅读(124) 评论(0) 推荐(0)
摘要:什么是顶层和底层const: 1. 顶层const:指const定义的变量本身是一个常量; 2. 底层const:指const定义的变量所指向的对象是一个常量; const int i = 0; //顶层const,变量i就是常量; const int * a = &i; //底层const, a所 阅读全文
posted @ 2021-03-26 08:57 唯一诺 阅读(462) 评论(0) 推荐(1)
摘要:class tmp { public: int a; int b; }; class ctest { public: ctest() { p = new tmp; } ~ctest() { delete p; } ctest& operator = (const ctest& t) { tmp * 阅读全文
posted @ 2021-03-25 20:26 唯一诺 阅读(96) 评论(0) 推荐(0)
摘要:int search(vector<int>& nums, int target) { int left = 0; int right = nums.size(); if (right == 0) { return -1; } bool flag = false; while (left<right 阅读全文
posted @ 2021-03-18 09:59 唯一诺 阅读(54) 评论(0) 推荐(0)
摘要:1.final 当希望类不被继承或者虚函数不被重写时,可以在类名后面或者函数后面使用final修改; 2.override 当表明此函数是重写而来时,可以用override修饰,此时编译器会检查是否为重写的函数; 阅读全文
posted @ 2021-03-17 08:35 唯一诺 阅读(158) 评论(0) 推荐(0)
摘要:Node* reveser(Node *head) { if (head == NULL) { return NULL; } Node* pre = NULL; Node* next = NULL; while (head != NULL) { next = head->next; head->ne 阅读全文
posted @ 2021-03-11 20:40 唯一诺 阅读(65) 评论(0) 推荐(0)
摘要:bool checkArr(char * arr, int len, int* max) { if (NULL == arr || len == 0 || max == NULL) { return false; } int maxdepth = 0, deep = 0; for (int i = 阅读全文
posted @ 2021-03-11 19:27 唯一诺 阅读(41) 评论(0) 推荐(0)
摘要:bool testFunc(int k) { char kai[] = { 1, 2, 1, 3, 4, 5, 1 }; int size = sizeof(kai); for (int i = 0; i < size; i++) { for (int j = 1; j <= k; j++) { i 阅读全文
posted @ 2021-03-11 11:20 唯一诺 阅读(66) 评论(0) 推荐(0)
摘要:ListNode* ReverseList(ListNode* head, ListNode* tail) { ListNode* pre = NULL; ListNode* next = NULL; while (head != tail) { next = head->next; head->n 阅读全文
posted @ 2021-03-10 19:06 唯一诺 阅读(58) 评论(0) 推荐(0)
摘要:int main(){ int arr[] = {4,2,3,5,6}; int n = sizeof(arr) / sizeof(int); int t = 0; for (int i = 1; i <= n+1; i++) { t ^= i; } for (int i = 0; i < n; i 阅读全文
posted @ 2021-03-05 09:08 唯一诺 阅读(730) 评论(0) 推荐(0)
摘要:ListNode* ReverseList(ListNode* pHead) { if (pHead == NULL) { return NULL; } ListNode pHeadNew(0); pHeadNew.next = pHead; ListNode* curNode = pHead; L 阅读全文
posted @ 2021-03-03 13:57 唯一诺 阅读(37) 评论(0) 推荐(0)
摘要:https://www.cnblogs.com/ooo0/p/12161786.html 值得一再强调的是,空间复杂度是根据额外需要的内存空间(也叫辅助空间)来算的,也就是说原本的数据不纳入计算。 尽管在第二个版本里我们有array这一入参,占用了N个元素的空间,但除此之外它并没有消耗额外的内存,所 阅读全文
posted @ 2021-03-02 20:18 唯一诺 阅读(93) 评论(0) 推荐(0)
摘要:__attribute__((constructor)) void before() { printf("this is main before\n"); } __attribute__((destructor)) void after() { printf("this is main after\ 阅读全文
posted @ 2021-03-02 09:05 唯一诺 阅读(628) 评论(0) 推荐(0)
摘要:#pragma pack(6)struct test{ char f[10]; //自身对齐值是1,指定对齐值是6,有效对齐值1 //f首地址0x0, 所在内存空间0x0-0x09 double z; //自身对齐值是8,指定对齐值是6,有效对齐值6 //z的首地址需要被有效值6整除,但是0x09之 阅读全文
posted @ 2021-03-01 19:07 唯一诺 阅读(453) 评论(0) 推荐(0)
摘要:转载:http://c.biancheng.net/view/422.html 程序运行时常会碰到一些异常情况,例如: 做除法的时候除数为 0; 用户输入年龄时输入了一个负数; 用 new 运算符动态分配空间时,空间不够导致无法分配; 访问数组元素时,下标越界;打开文件读取时,文件不存在。 这些异常 阅读全文
posted @ 2020-11-12 19:34 唯一诺 阅读(268) 评论(0) 推荐(0)
摘要:1.对于单纯常数量,最好使用const对象或者enum替换#defines 2.对于长的像函数的宏定义,最好改用inline内联函数替换#define 阅读全文
posted @ 2020-10-29 20:02 唯一诺 阅读(90) 评论(0) 推荐(0)
摘要:1.如果类A可能会被继承,那么A的析构函数一定要被设置为virtual虚函数; 原因:如果基类的析构函数不是虚函数,当创建一个基类指针指向一个派生类对象,当释放此基类指针时,子类的析构函数不会被调用; 如下: class CFather { public: CFather(); virtual ~C 阅读全文
posted @ 2020-10-21 09:04 唯一诺 阅读(744) 评论(0) 推荐(0)
摘要:class CFather { public: CFather(); virtual ~CFather(); public: virtual void test(); //virtual void getHouse() = 0; public: int b; const static int c = 阅读全文
posted @ 2020-10-21 08:50 唯一诺 阅读(138) 评论(0) 推荐(0)