- 博客(7)
- 收藏
- 关注
原创 有虚函数的类的析构函数必须定义为虚构函数
如果不定义为虚析构函数,那么,基类指针指向派生类对象,删除指针的时候,就会直接调用基类的析构函数,那么派生类的虚构函数会被跳过,从而造成内存泄漏。 不仅仅是有虚函数的类,凡是用来实现多态的基类的析构函数都应该是虚析构函数,否则会造成内存泄漏。...
2018-08-07 18:15:38
1925
原创 类的const成员赋值
如果一个类他有一个const 成员变量,那么只能在参数初始化列表中为它赋值。原因:因为常量只能初始化,不能赋值,所以不能在构造函数中为它赋值,(相当于到达构造函数体内时,成员变量已经初始化完了,就相当于不能给const变量改值一样)class A {public: A(int b) :a(b) {} const int a;};class B {public: B(int ...
2018-08-07 18:05:04
1137
1
原创 写一个程序判断机器是大端存储还是小端存储
0x11223344大端:11 22 33 44 小端:44 33 22 11#include<iostream>using namespace std;int main(){ int i=0x11223344; char *c = (char*)&i; if(*c==0x44){ cout<&l...
2018-08-06 14:05:19
1300
原创 c++进程内存空间分布
c++进程内存空间分布(注意各部分的内存地址谁高谁低,注意栈从高到低分配,堆从低到高分配) 内存分布分为5个部分,从高地址到低地址一次为 栈区(stack),堆区(heap),未初始化数据段(uninitialized data),初始化数据段(initialize data),代码段(text)。1.文本段也叫代码段,是对象文件或内存中程序的一部分,其中包含可执行指令。文本段...
2018-08-06 13:49:52
4986
原创 c++ int和string互相转化
int 转 string:#include <sstream>string int_to_string(const int i){ stringstream stream; stream<<i; string s; stream >> s; return s;}string 转 int:#include <s...
2018-06-12 15:36:49
232
原创 二叉树三种遍历方法的递归与非递归实现
二叉树的三种递归方法:前序、中序和后序。前序遍历:先根节点,再左孩子,最后右孩子。递归实现://前序递归void PreorderRecusively(BinaryTree *root){ if(root == NULL) return; coutval<<endl; PreorderRecusively(root->lchild); PreorderRecusively
2017-10-31 15:20:20
306
原创 Pascal's Triangle II c++
原题目:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?
2017-10-24 10:35:28
293
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人