网站统计查询,在淘宝做网站可以退货退款么,简单网页设计模板代码,自适应网页设计规范内存管理
new/delete
C语言 malloc free完成对堆内存的申请和释放。
C new delete 类
new#xff1a;动态申请存储空间的运算符#xff0c;返回值为申请空间的对应数据类型的地址
int *p new int(10); 申请了一个初始值为10的整型数据
int *p new int[10]; 申…内存管理
new/delete
C语言 malloc free完成对堆内存的申请和释放。
C new delete 类
new动态申请存储空间的运算符返回值为申请空间的对应数据类型的地址
int *p new int(10); 申请了一个初始值为10的整型数据
int *p new int[10]; 申请了能存放10个整型数据元素的数组其首地址为arr 单变量空间
#include iostream
#include stdlib.h
using namespace std;//malloc free # include stdlib.h 库函数
//new delete key work 关键字int main()
{//Cint *p (int*)malloc(sizeof(int));int *p static_castint*(malloc(sizeof(int)));//C 单变量空间int *p new int(200);//*p 200;cout*pendl;string *ps new string(aaa);//*ps china;cout*psendl;struct Stu{int age;string name;};Stu *pStu new Stu{10, bob};coutpStu-ageendl;coutpStu-nameendl;return 0;
}
多变量空间 数组
#include iostream
#include string.h // #include cstring
#include stdlib.h
using namespace std;int main()
{char* p new char[4];const char* source aa;strcpy_s(p, 4, source);cout p: p endl;int *pi new int[5]{0};memset(pi, 0, sizeof(int[5]));for(int i 0; i 5; i){coutpi[i]endl;}char **ppc new char*[5]{NULL};ppc[0] new char[10];strcpy(ppc[0], china);ppc[1] automan;ppc[2] greatwall;while(*ppc){cout*ppcendl;}return 0;
}
一维、多维
#include iostream
#include stdlib.h
#include string.h
using namespace std;int main()
{int(*pa)[4] new int[3][4]{ {0} };for (int i 0; i sizeof(int[3][4]) / sizeof(int[4]); i){for (int j 0; j 4; j){cout pa[i][j] ;}cout endl;}int (*px)[3][4][5] new int[2][3][4][5];return 0;
} 内存释放
#include iostream
#include stdlib.h
#include string.h
using namespace std;int main()
{int *p new int;delete p;int *q new int[1000];delete []q;//多维只用一个框即可内核用递归删除int *r new int[1000][][];delete []r;return 0;
} 内联函数
内联函数inline function
介于宏函数和普通函数之间
宏函数
优点代码内嵌避免了函数调用。
缺点容易产生歧义易使text段体积增大。
普通函数
优点一段高度抽象的逻辑不易产生歧义使text段体积减小。
缺点函数调用的压栈与出栈的开销。
inline 内联函数
优点一段高度抽象的逻辑不易产生歧义使text段体积减小会进行类型检查避免压栈与出栈的开销。
代价增加代码段的空间
本质以牺牲代码段空间为代价提高程序的运行时间的效率
适用代码体很小且频繁调用
为何不把所有函数inline
内嵌太多inline变成了给编译器的一种建议
只有当函数只有10行甚至更少时才会将其定义为内联函数。
#include iostream
using namespace std;#define SQR(i) ((i)*(i)) //宏函数int sqr(i) //普通函数
{return i * i;
}inline int sqr(i)
{return i * i;
}int main()
{int i 0;while(i 5){coutSQR(i)endl;}return 0;
} 强制类型转换
#include iostream
#include stdlib.husing namespace std;void func(int v)
{coutvendl;
}int main()
{static_cast 对于隐式类型可以转化的即可用此类型float a 5.6;int b 5;//隐式类型转换a b;b a;b static_castint(a);a static_castfloat(b);void *p; int *q;p q;q p; //报错q static_castint*(p);int x 10;int y 3;float z static_castfloat(x) / y;char * pc static_castchar*(malloc(100));reinterpret_cast 对于无隐式的类型转化static_cast不可用char * p; int * q;p reinterpret_castchar*(q);int a[5] {1, 2, 3, 4, 5};int *p (int*)((int)a1);int *p reinterpret_castint*((reinterpret_castint(a) 1));couthex*pendl;const_cast 脱常只能应用于指针和引用const 修饰的一定不可以改const int a 19;func(const_castint(a));dynamic_castreturn 0;
}
宏在预处理阶段发生了替换
常量编译阶段发生了替换
常量不变 命名空间
命名空间为大型项目开发避免命名冲突的一种机制。
:: 作用域运算符前面要命名空间
全局无名命名空间
局部
namespace 是对全局命名空间的再次划分。
#include iostreamusing namespace std;int v 55; // 全局int main()
{int b 10; // 局部int *p v;coutvendl;coutbendl;cout::endl;return 0;
} #include iostreamusing namespace std;namespace Space{int x;void func(){printf(void func);}struct Stu{int a;int b;}
}namespace Other{int x;int y;
}int main()
{Space::x 200;coutSpace::xendl;using Space::x;x 20;coutxendl;using namespace Space;Stu s {1, 2};cout s.a --- endl;using namespace Other;Other::x 10;y 20;coutOther::xyendl;int m, n;std::cinmn;std::coutmnstd::endl;return 0;
}
如果有局部变量名相同冲突 支持嵌套
#include iostreamusing namespace std;namespace Space{int a;int b;namespace Other{int m;int n;}
}int main()
{using namespace Space::Other;m 20;return 0;
}
协作开发
#include iostreamusing namespace std;namespace Space
{int x;
}namespace Space
{int y;
}int main()
{using namespace Space;int x 10;int y 20;coutxyendl;return 0;
}
相同空间名会合并 String类
#include iostreamusing namespace std;//string 不是关键字而是一个类int main()
{std::string str;string str(china);string str china;str good;string str2(str);coutstrendl;coutstr2endl;string s china;s[3] w;coutsendl;char buf[1024];strcpy(buf, s.c_str()); //string - char* c_str返回字符串coutbufendl;str.swap(str2); //交换两个字符串 swap 成员函数int n str.find(i, 0); //查找一个字符的位置返回下标找不到返回-1coutn nendl;string sArray[10] {0,1,22,333,4444,55555,666666,7777777,88888888,999999999,};for(int i 0; i 10; i){coutsArray[i]endl;}return 0;
} 总结
malloc free C库函数 new delete new[] delete[] 关键字
new delete malloc free 申请单变量空间
申请数组 一维 多维
#include iostreamusing namespace std;struct Str
{char *p;
};int main()
{string *ps new string;*ps china;coutpsendl; //输出地址 对象的地址cout*psendl; //输出值 对象的内容struct Str str {abcdefg};int *pi new int[10]{0};char **ppc new int*[5]{NULL}; //定义指针数组int (*p)[4] new int[3][4];return 0;
}
erase(0, npos)
从0开始一直删除到 位置
str.erase(0, str.find_first_not_of( ));
下标后往后删除
str.erase(str.find_last_not_of( ) 1); #include iostream
#include string.h
#include stdlib.husing namespace std;int main()
{FILE *fp fopen(aa.txt, r); //打开并读取文件if(fp NULL)return -1;vectorstring vs;char buf[1024];while(fgets(buf, 1024, fp) ! NULL) //读取文件内容{vs.push_back(buf); // 内容接在后边}for(int i 0; i vs.size(); i){coutvs[i]endl;}fclose(fp);return 0;
}