C++中的结构体不再需要struct关键字,直接用结构体名即可,并在结构体中允许有函数存在。
#includeusing namespace std; struct MM { //结构体中的数据称之为:属性或特征,通俗称之为:数据成员 char name[20]; int age; //结构体中的函数称之为:行为或方法,通俗称之为:成员函数 void print() { cout << name << 't' << age << endl; } }; int main() { struct MM girl = {"张三",20}; MM mm; //不需要struct关键字 gril.print(); //调用结构体里的函数 return 0; }
//C++在没有写构造函数和权限限定的时候,和C语言的用法是一样的
动态内存申请
C++的动态内存申请 : new(申请) 和 delete(释放)
单词内存的申请:
#includeusing namespace std; int main() { //单个内存申请 //申请不做初始化 int *pInt = new int; *pInt = 123; cout << *pInt << endl; //申请做初始化 用()给单个数据赋值 char *pChar = new char('A'); cout << *pChar << endl; //释放 delete pInt; pInt = nullptr; delete pChar; pChar = nullptr; //再次使用pInt *pInt = new int (321); cout << *pInt << endl; //再次释放 delete pInt; pInt = nullptr; return 0; }
数组内存申请:
#include#include using namespace std; int main() { //一位数组 //1.不带初始化申请 int *pInt = new int[3]; //等效产生了 int pInt[3] char* pstr = new char[15]; strcpy_s(pstr,15,"ILoveYou"); cout << pstr << endl; //带初始化的 一堆数据用{} int *pNum = new int[3]{1,2,3}; for(int i = 0;i < 3; i++) { cout << pNum[i] << 't'; } cout << endl; //释放 //释放只有两种形式 delete 指针;delete [] 指针 delete [] pInt; //数组的释放 不需要大小 pInt = nullptr; delete [] pstr; pstr = nullptr; delete [] pNum; pNum = nullptr; }
结构体内存申请:
#include#include struct MM { char* name; int age; void print() { cout << name << 't' << age << endl; } } int main() { //new 一个对象 //结构体只能用大括号 但不建议使用 //MM* pMM = new MM{}; MM* pMM = new MM; //结构体中指针,要二次申请,才能strcpy,或者赋值 pMM->name = new char[20]; strcpy_s(pMM->name,20,"莉丝"); pMM->age = 18; pMM->print(); //申请顺序和释放顺序是相反的 delete []pMM-name; delete pMM; return 0; }
内存池
内存池允许我们申请一段内存,共给程序使用,综合管理内存
#includeusing namespace std; //允许大家申请一段内存,共给程序使用,综合管理内存 //malloc 内存是在堆区 //new 内存是自由存储区 int main() { char* memorySum = new char[1024]; // ......事情的处理需要内存,所有内存源自于memorySum int* pNum = new(memorySum) int[3]{1,2,3}; for(int i = 0; i <3; i++) { cout << pNum[i] << 't'; } //char* pstr = new(pNum + 3) char[20]{"ILoveYou"}; char* pstr = new(memorySum + 12) char[20]{"ILoveYou"}; for(int i = 0; i< 20;i++) { cout << ((int *)memorySum[i]) << endl; } //释放 delete [] memorySum; memorySum = nullptr; return 0; }
string类型
string创建:
#include#include using namespace std; int main() { //不带初始化 //一般用string 不会用 const修饰 string str; str = "hello" cout << str << endl; //带初始化 //用赋值号 = 赋值 strint str1 = "ILoveYou"; cout << str1 << endl; //用小括号 () 赋值 string str2("IMissYou); cout << str2 << endl; //用另一个字符串创建 string str3 = str2; cout << str3 << endl; string str4(str3); cout << str4 << endl; //string一般没有长度 string str5 = “19246187687126487216487326487648238572398752981”; return 0; }
string类型的基本操作:
#include#include using namespace std; int main() { string str1 = "one"; string str2 = "two"; string str3 = str2; cout << str3 << endl; //两个字符串的拼接 //没有减肥只有 + string srt4 = str1 + str2; cout << str4 << endl; //字符串的比较 //比较依旧安装char* 去比较 if(str1 > str2) { cout << "str1 大!" << endl; } else { cout << "str2 大!" << endl; } return 0; }
C++的string 和 C语言中的string
C++中的string时一个自定义类型;不能用到C语言的字符串处理函数。
C++中的string需要用到c_str() 和 data() 转换为C语言的char*
C++中string的结尾没有记录
#include#include #include #include using namespace std; int main() { string str1 = "ILoveYou"; //printf("%sn",str); 无法输出 printf("%sn",str1.c_str()); printf("%sn",str1.data()); //直接把数字转换为相应的字符串 string str2 = to_string(12345); cout << str2 << endl; return 0; }
其他函数操作:
emepty() 判断是否为空函数
size() 测试长度 函数
#include#include using namespace std; int main() { string mystring = "IMissYou"; cout << "mystring长度为: " <



