时间:2022-08-08 08:21:41 | 栏目:C代码 | 点击:次
#include <iostream> #include<string> using namespace std; struct MM { char name[20]; int age; }; int main() { struct MM girl; MM mm;//C++中不需要struct关键字 return 0; }
#include <iostream> #include<string> using namespace std; struct MM { //属性 //数据成员 char name[20]; int age; //行为(方法) //成员函数 void print() { cout << name << "\t" << age << endl; } void printData();//在结构体中声明,在外面实现 int& getAge() { return age; } }; //结构体名限定,就是告诉别人这个函数来自哪里 void MM::printData() { cout << name << "\t" << age << endl; } //结构体中的变量必须要通过结构体变量(结构体指针)访问 //c++结构体中的函数访问属性,可以直接访问 int main() { struct MM girl = {"小芳",28}; MM mm = {"小丽",24};//C++中不需要struct关键字 girl.print(); (&mm)->printData(); MM* p = &mm; p->printData(); p->getAge() = 84; p->printData(); p->age = 1991; p->printData(); return 0; }
例子:单个变量内存申请和数组的动态申请
#include<iostream> #include<string> using namespace std; void testNoeMemory() { //申请不做初始化 int* pInt = new int; *pInt = 123; cout << *pInt << endl; char* pChar = new char; *pChar = 'A'; cout << *pChar << endl; //申请内存做初始化 ()给单个数据做初始化 int* pNum = new int(134); cout << *pNum << endl; delete pInt; pInt = nullptr; delete pChar; pChar = nullptr; delete pNum; pNum = nullptr; } void testArrayMerrmory() { //一维数组 //1、不带初始化 //长度可以是h变量,只要值就可以 int* pInt = new int[3];//等效产生了 int pInt[3]的数组 char* pstr = new char[15]; strcpy_s(pstr, 15, "I love you"); cout << pstr << endl; //带初始化的 一堆数据用{} int* pNum = new int[3]{1, 2, 3}; for (int i = 0; i < 3; i++) { cout << pNum[i] << " "; } cout << endl; delete[] pNum; char* str = new char[20]{'A', 'B', '\0'}; cout << str << endl; delete[] str; str = nullptr; str = new char[20]{"Ilove you"}; cout << str << endl; delete[] str; str = nullptr; delete[] pInt;//数组的指针 不需要大小 //释放只有两种形式 delete 指针 delete [] 指针 //delete [][] p 没有这种写法 pInt = nullptr; } int main() { testNoeMemory(); return 0; }
例子:结构体内存申请
#include<iostream> #include<string> using namespace std; void testNoeMemory() { //申请不做初始化 int* pInt = new int; *pInt = 123; cout << *pInt << endl; char* pChar = new char; *pChar = 'A'; cout << *pChar << endl; //申请内存做初始化 ()给单个数据做初始化 int* pNum = new int(134); cout << *pNum << endl; delete pInt; pInt = nullptr; delete pChar; pChar = nullptr; delete pNum; pNum = nullptr; } void testArrayMerrmory() { //一维数组 //1、不带初始化 //长度可以是h变量,只要值就可以 int* pInt = new int[3];//等效产生了 int pInt[3]的数组 char* pstr = new char[15]; strcpy_s(pstr, 15, "I love you"); cout << pstr << endl; //带初始化的 一堆数据用{} int* pNum = new int[3]{1, 2, 3}; for (int i = 0; i < 3; i++) { cout << pNum[i] << " "; } cout << endl; delete[] pNum; char* str = new char[20]{'A', 'B', '\0'}; cout << str << endl; delete[] str; str = nullptr; str = new char[20]; cout << str << endl; delete[] str; str = nullptr; delete[] pInt;//数组的指针 不需要大小 //释放只有两种形式 delete 指针 delete [] 指针 //delete [][] p 没有这种写法 pInt = nullptr; } struct MM { char* name; int age; void printMM() { cout << name << "\t" << age << endl; } }; void testStructMerrory() { //new一个对象 int* p = new int(123); //结构体只能用大括号 MM* pMM = new MM; //结构体中指针,要做二次申请,才能strcpy,或者赋值 pMM->name = new char[20]; strcpy_s(pMM->name, 20, "李四"); pMM->age = 188; pMM->printMM(); //申请的顺序和释放的顺序是相反的 delete[]pMM->name; delete pMM; } int main() { //testNoeMemory(); testStructMerrory(); return 0; }
允许大家申请一段内存,共给程序使用,综合管理内存
只需要知道有这种用法即可,不需要大家深究为什么,因为string本身是一个类,需要讲完类的大部分知识,才能追究为什么这样做。自己也可以封装一个string 类型
+ 带初始化
+ 不带初始化
+ 通过另一个字符串创建
+ 拷贝
+ 赋值
+ 连接
+ 比较