C++与C的区别
1结构体区别
1.1:类型不再使用struct关键字,直接用结构体名即可
1.2:C++结构体中允许函数的存在
1.2.1在结构体中声明,在结构体或者内外实现
1.2.2结构体中函数访问数据,可以直接访问
1.2.3学会调用,和数据成员方式一样
对象(结构体变量).成员
对象指针->成员
(*对象指针).成员
C++在没有写构造函数和权限限定时,用法和C语言的用法是一样的
#include <iostream>
using namespace std;//命名空间的使用
struct student
{
char name[10];//在C++中把数据叫做属性,特征,结构体中的数据成员
int age;
float score;
void print()//在C++中把函数叫做行为,结构体中成员函数
{
cout << name << "\t" << age << score << endl;
}
};
void student::print()//在外部调用,用到结构体名限定,标明函数来自哪里
{
}
//结构体中的变量必须要通过结构体变量访问
//C++结构体中的函数访问属性,可以直接反访问
int main()
{
struct student A = { "哈皮",18,99 };//A就是一个对象
student B = { "嗨皮",18,99 };
A.print();//数据的几种访问形式
(&B)->age;
student* p = &A;
p->name;
return 0;
}
2动态内存申请
让我们先回顾一下C语言的动态内存申请吧
malloc 不带初始化 calloc 带初始化 realloc重新申请 free释放内存
2.1C++的动态内存申请
new(申请)delet(释放)
单个变量内存申请 数组的动态申请 结构体内存申请
#include <iostream>
using namespace std;//命名空间的使用
void testOneMenory()//单个变量内存申请
{
//申请内存不做初始化
int* pInt = new int;//new的使用
*pInt = 999;
cout << *pInt << endl;
char* pChar = new char;//new的使用
*pChar = 'A';
cout << *pChar << endl;
//申请内存做初始化
int* pNum = new int(999);//加个小括号就行啦
cout << *pNum<< endl;
delete pInt;//有申请就有释放,大家不要忘了释放内存哦
pInt = nullptr;
delete pChar;//delete的使用
pChar = nullptr;
delete pNum;//delete的使用
pNum = nullptr;
}
void testArrayMemory()//数组的动态申请
{
//一维数组
//不带初始化
int* pInt = new int[3];
char* pInt1 = new char[15];
cout << pInt << endl;
//带初始化
int* pNum = new int[3]{ 1,2,3 };
for (int i = 0; i < 3; i++)
{
cout << pNum[i] << " ";
}
cout << endl;
char* str = new char[20]{ 'a','A','M','\0' };
cout << str << endl;
str = new char[20]{ "hhhhhh" };
delete[]pInt;//注意这里的内存释放要加[]表明是一段内存
pInt = nullptr;
delete[]pInt1;//数组的释放不管几维数组只加一个[]
pInt1 = nullptr;
delete[]pNum;
pNum = nullptr;
delete[]str;
str = nullptr;
}
struct student
{
char name[10];//在C++中把数据叫做属性,特征,结构体中的数据成员
int age;
float score;
void print()//在C++中把函数叫做行为,结构体中成员函数
{
cout << name << "\t" << age << score << endl;
}
};
void testStructMemory()//结构体动态内存申请
{
//先new一个对象
int* p = new int(23);
//结构体用大括号初始化
student* pA = new student{ "嗨皮",18,99 };
pA->print();
delete[]pA;
pA = nullptr;
}
3内存池
萌新是这样理解的:先申请一段大内存,再在大内存中一小段一小段用,最后只释放大内存就好啦
#include <iostream>
using namespace std;//命名空间的使用
//内存池,允许申请一段内存,供程序使用,综合管理内存
//malloc内存是在堆区
//new内存是自由存储区
void testMemory()
{
char* memorySum = new char[1024];
int* pNum = new(memorySum) int[3]{ 1,2,3 };
char* pStr = new(memorySum + 3 * 4) char[20]{ "hhhhhh" };
//上一行与下一行等效,下一行是指针的偏移
char* pStr = new(pNum + 3) char[20]{ "hhhhhh" };
cout << (memorySum + 3 * 4) << endl;
delete memorySum;
memorySum = nullptr;
}
4string类型
4.1string创建 带初始化 不带初始化 通过另一个字符串创建
#include <iostream>
#include <string>//包含头文件
using namespace std;//命名空间的使用
void createString()
{
string str1;//不带初始化
str1 = "hhhhhh";
cout << str1 << endl;
string str2("hhhhhh");//带初始化
string str3 = "hhhhhhhh";
cout << str2 << str3 << endl;
string str4(str3);//通过另一个字符串创建str4=str3
cout << str4;
}
4.2string基本操作 拷贝 赋值 连接 比较
4.3C++string与C语言string.h
void operateString()
{
string str1 = "hhh";//赋值
string str2 = "lll";
string str3 = str1 + str2;//连接
if (str1 == str2) {//比较
cout << "这两个字符串不相同" << endl;
}
//用printf输出
printf("%s\n", str1.c_str());
printf("%s\n", str1.data());
//to_string()用法
str2 = to_string(9999);//把数字转换为字符串
cout << str2 << endl;
}
void exOpeartor()
{
string str = "Long time no see";
for (int i = 0; i < 8; i++)
{
cout << str[i];
}
cout << "str的长度:" << str.size() << endl;
//str.capacity()str的容量
}
二维数组的动态内存申请,采用子函数的方式为二级指针申请内存并释放内存:
作业:
#include <iostream>
using namespace std;//命名空间的使用
int** homework(int X,int Y)
{
int** p = new int*[X];
for (int i = 0; i < X; i++) {
p[i] = new int[Y];
}
/*
int** a;
int i;
a = (int**)malloc(sizeof(int*) * 3);//为二维数组分配3行
for (i = 0; i < 3; ++i) {//为每列分配4个大小空间
a[i] = (int*)malloc(sizeof(int) * 4);
}*/
return p;
}
int main()
{
int** M = homework(2, 3);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
M[i][j] = i * j;
cout << M[i][j] << " ";
}
}
return 0;
}
输出结果:
0 0 0 0 1 2
C:\Users\86137\source\repos\C++\Debug\C++.exe (进程 15132)已退出,代码为 0。
按任意键关闭此窗口. . .
好啦,就先到这里啦。