1.结构体
-
类型上不再需要struct关键字,直接用结构体名即可
-
C++结构体中允许函数存在
-
在结构体中声明,在结构体外实现,当然可以直接在结构体中实现
-
结构体中函数访问数据,是可以直接访问
-
学会调用,和数据成员方式时一样的
-
对象(结构体变量).成员
-
对象指针->成员
-
(*对象指针).成员
-
-
C++在没有写构造函数和权限限定的时候,用法和C语言的用法是一样
-
-
#include<iostream> #include <cstring> using namespace std; struct student { char name[10]; char stuNum[10]; int age; /*C++结构体中允许函数存在 可以在结构体中声明, 在结构体外实现,当然可以直接在结构体中实现*/ void print();//在结构体中声明 //通过外部函数来修改数据 int& modifyAge() { return age; } }; //在外面实现 void student::print() { //student::是结构体名限定,表明函数来自那个结构体 cout << name << " " << stuNum << " " << age << endl; } int main() { struct student stu1 = { "张三","1001",21 }; stu1.print(); student stu2 = { "李四", "1002",22 }; stu2.print(); //结构体中的变量必须要通过结构体变量(结构体指针)访问 //C++结构体中的函数访问属性,可以直接访问 //1.对象(结构体变量).成员访问 stu1.age = 21; strcpy(stu1.name, "周六"); stu1.print(); stu1.modifyAge()=18; stu1.print(); //2.对象指针->成员访问 (&stu1)->age = 22; strcpy((&stu1)->name, "王五"); stu1.print(); (&stu1)->modifyAge() = 19; stu1.print(); return 0; }
2.动态内存申请
-
C语言的动态内存申请
-
malloc 不带初始化 ,calloc 带初始化,realloc 重新申请
-
free 释放
-
-
C++的动态申请
-
new(申请)和delete(释放)
-
单个变量内存申请
-
数组的动态申请
-
结构体内存申请
-
-
#include<iostream> #include <cstring> using namespace std; //动态申请单个数据 void testOneMemory() { //整型 int* pInt = new int; *pInt = 12; cout << *pInt << endl; int* pInt1 = new int(11);//申请内存做初始化 用()给单个数据初始化 cout << *pInt1 << endl; //整型 char* pChar = new char; *pChar = 'a'; cout << *pChar << endl; char* pChar1 = new char('b'); cout << *pChar1 << endl; //释放内存,指针置空 delete pInt; pInt = nullptr; delete pInt1; pInt1 = nullptr; delete pChar; pChar = nullptr; delete pChar1; pChar1 = nullptr; } //动态申请数组 一维数组 void testArrayMemory() { //1.不初始化 //整型数组 int* pArray = new int[10];//相当于数组 int pArray[10] for (int i = 0; i < 10; i++) { pArray[i] = i; } for (int i = 0; i < 10; i++) { cout << pArray[i] << " "; } cout << endl; delete[]pArray; pArray = nullptr; //字符数组 const char* pStr = new char[20]; const char* pStr1 = pStr; pStr = "Hello World"; /*数据并没有传入申请的内存中,只是改变了pStr 的指向,使pStr指向"Hello World"所在的常量区 */ cout << pStr << endl;//输出 Hello World cout << pStr1 << endl;//输出垃圾值 char* pStr2 = new char[20]; char* pStr3 = pStr2; delete[]pStr1; pStr = nullptr; pStr1 = nullptr; //使用strcpy向申请的内存中赋值 strcpy_s(pStr2, 20, "Hello World"); cout << pStr2 << endl;//输出 Hello World cout << pStr3 << endl;//输出 Hello World delete[]pStr2; pStr2 = nullptr; pStr3 = nullptr; //2.初始化 用{}进行初始化 //整型 int* pArray1 = new int[10]{ 1,3,3,3,4,4 }; for (int i = 0; i < 10; i++) { cout << pArray1[i] << " "; } cout << endl; delete[]pArray1; pArray1 = nullptr; //字符数组 char* pstr = new char[20]{ 'a','1','r','4' }; for (int i = 0; i < 20; i++) { cout << pstr[i] << " "; } cout << endl; delete[]pstr; pstr = nullptr; //释放只有两种形式 delete 指针 delete [] 指针 } void testArrayMemory2D() { //3行4列 int** pArray = new int* [3];//3行 申请3个1级指针 for (int i = 0; i < 3; i++) { pArray[i] = new int[4];//4列 每个1级指针指向一个由4个元素的数组 } for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { pArray[i][j] = i + j; cout << pArray[i][j] << " "; } cout << endl; } delete[]pArray; } //动态申请结构体 struct student { //char name[10]; char* name; char stuNum[10]; int age; void print() { cout << name << " " << stuNum << " " << age << endl; } }; void testStruct() { student* pStu = new student; pStu->name = new char[10]; strcpy(pStu->name, "张三"); strcpy(pStu->stuNum, "1001"); pStu->age = 21; pStu->print(); delete[]pStu->name; delete pStu; pStu = nullptr; pStu = nullptr; } int main() { //testOneMemory(); //testArrayMemory(); //testArrayMemory2D(); testStruct(); return 0; }
3.
3.内存池
-
申请一段内存,共给程序使用,综合管理内存
-
#include<iostream> #include <cstring> using namespace std; int main() { char* pSum = new char[1024]; int* pInt = new(pSum) int[4]; for (int i = 0; i < 4; i++) { pInt[i] = i; } char* pChar = new(pInt + 4) char[20]{ "Hello World" }; //char* pChar = new(pSum + 16) char[20]{ "Hello World" }; for (int i = 0; i < 4; i++) { cout<<((int*)pSum)[i]<<" "; } cout << endl; cout << pChar << endl; delete[]pSum; pSum = nullptr; pChar = nullptr; pInt = nullptr; return 0; }
1
4.string类型
-
#include <iostream> #include <string> using namespace std; //string的创建 void createString() { string str; str = "Helloworld"; cout << str << endl; //创建的时候初始化 string str1 = "张三"; string str2("李四"); string str3 = str; cout << str1 << endl << str2 << endl<<str <<endl; } //string基本操作 void operateString() { string str1 = "张三"; string str2 = "李四"; string str3 = str1+str2; string str4 = str1.append(str2); cout << str3 << endl << str4 << endl; str1 > str2 ? cout<<"str1大" << endl : cout << "str2大" << endl; str1.compare(str2)? cout << "str1大" << endl : cout << "str2大" << endl; } void translateString() { //C++string 不能用到C语言的字符串处理函数 //C++调用c_str()或data()函数转换为C语言的char* string str = "Hello World"; printf("%s\n", str.c_str()); printf("%s\n", str.data()); //直接把数字转换为相应的字符串 string str1 = to_string(12345678); cout << str1 << endl; } void CommonString() { string str = "Hello World"; cout << "字符串长度:" << str.size() << endl;//不计算'\0' string str1 = ""; cout << "字符串长度:" << str1.size() << endl;//不计算'\0' if (str1.empty()) { cout << "str1为空" << endl; } } int main() { //createString(); //operateString(); //translateString(); CommonString(); return 0; }
5.编程题
- 二维数组的动态内存申请,采用子函数的方式 为二级指针申请内存,和释放内存
-
#include<iostream> using namespace std; int** createpArray2D(int row, int cols) { int** pArray = new int* [row]; if (pArray == nullptr) return nullptr; for (int i=0;i<row;i++) { pArray[i] = new int[cols]; if (pArray[i] == nullptr) return nullptr; } //return pArray; //遍历数组 for (int i = 0; i < row; i++) { for (int j = 0; j < cols; j++) { pArray[i][j] = i + j; cout << pArray[i][j] << " "; } cout << endl; } //释放内存 for (int i = 0; i < row; i++) { delete pArray[i]; pArray[i] = nullptr; } delete []pArray; pArray = nullptr; } int main() { createpArray2D(3, 5); return 0; }