1. 结构体区别
1.1. 类型上不再需要struct关键字,直接用结构体名即可
#include <iostream>
#include<string>
using namespace std;
struct MM
{
char name[20];
int age;
};
int main()
{
struct MM girl;
MM mm;
return 0;
}
1.2. C++结构体中允许函数存在
- 在结构体中声明,在结构体外实现,当然可以直接在结构体中实现
- 结构体中函数访问数据,是可以直接访问
- 学会调用,和数据成员方式时一样的
- 对象(结构体变量).成员
- 对象指针->成员
- (*对象指针).成员
- C++在没有写构造函数和权限限定的时候,用法和C语言的用法是一样
#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;
}
int main()
{
struct MM girl = {"小芳",28};
MM mm = {"小丽",24};
girl.print();
(&mm)->printData();
MM* p = &mm;
p->printData();
p->getAge() = 84;
p->printData();
p->age = 1991;
p->printData();
return 0;
}
1. 动态内存申请
C语言的动态内存申请
- malloc 不带初始化 ,calloc 带初始化,realloc 重新申请
- free 释放
C++的动态申请
- new(申请)和delete(释放)
- 单个变量内存申请
- 数组的动态申请
- 结构体内存申请
例子:单个变量内存申请和数组的动态申请
#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()
{
int* pInt = new int[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;
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()
{
int* pInt = new int[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;
pInt = nullptr;
}
struct MM
{
char* name;
int age;
void printMM()
{
cout << name << "\t" << age << endl;
}
};
void testStructMerrory()
{
int* p = new int(123);
MM* pMM = new MM;
pMM->name = new char[20];
strcpy_s(pMM->name, 20, "李四");
pMM->age = 188;
pMM->printMM();
delete[]pMM->name;
delete pMM;
}
int main()
{
testStructMerrory();
return 0;
}
2. 内存池
允许大家申请一段内存,共给程序使用,综合管理内存
3. string类型
只需要知道有这种用法即可,不需要大家深究为什么,因为string本身是一个类,需要讲完类的大部分知识,才能追究为什么这样做。自己也可以封装一个string 类型
- string创建
+ 带初始化 + 不带初始化 + 通过另一个字符串创建 - string基本操作
+ 拷贝 + 赋值 + 连接 + 比较 - C++string与C语言string.h
- string 其他函数操作
|