C++与C语言02
结构体区别 11-18
类型上不再需要struct关键字,直接用结构体名即可
结构体的创建:
#include"iostream"
using namespace std;
struct Struct {
int data;
};
int mian()
{
Struct ST;
while (1);
return 0;
}
C++结构体中允许函数存在
#include"iostream"
using namespace std;
struct Struct {
int data;
void Function() {
cout << "I am a function in struction\n";
}
};
int main()
{
Struct ST;
ST.Function();
while (1);
return 0;
}
访问:
- 对象(结构体变量).成员
- 对象指针->成员
- (*对象指针).成员
动态内存申请
-
C语言的动态内存申请
- malloc 不带初始化 ,calloc 带初始化,realloc 重新申请
- free 释放
-
C++的动态申请
-
new(申请)和delete(释放)
-
单个变量内存申请
-
数组的动态申请
-
结构体内存申请
-
all codes
#include"iostream"
using namespace std;
struct Struct {
int data; // 不能硬cout 吗?
void Function() {
cout << "I am a function in struction\n";
}
};
void Testmemory()
{
int* p = new int;
*p = 0;
cout << " Testmemory()" << endl;
*p = 1024;
cout << *p << endl; // * 千万别忘了
delete p;
cout << "单个变量内存申请,complete" << endl;
// 数组的动态申请
int* nums = new int[10];
for (int i = 0; i < 5; i++)
nums[i] = i * i;
for (int i = 4; i >= 0; i--)// 不能取 5 ,从0 开始
cout <<nums[i]<<endl;//cout << *(nums + i);
// 结构体内存申请
Struct *STP = new Struct;
STP->data = 949494;
}
int main()
{
Struct ST;
ST.Function();
Testmemory();
while (1);
return 0;
}
内存池
你自己先拿一个大大的囊分给你朋友;
具体怎么分按你的意愿来;
#include"iostream"
using namespace std;
void memorypool()
{
void* mpool = new char(1024);// 有了池子
int* pNum = new (mpool) int;
*pNum = 1111;
cout << "池子中第一个段"<< * pNum << endl;
const char* ch = new (mpool) char[100];
ch = "ILOVEYOU BECAUSE NOBODAY CAN LOVEMEHHHHHHH";
cout << ch << endl;
}
int main()
{
memorypool();
return 0;
}
string类型
-
string创建
- 带初始化
-
string str2("ILoveyou");
- 不带初始化
- string str1;
- 通过另一个字符串创建’
-
string基本操作
- 拷贝
- 赋值
- 连接
- 比较
-
C++string与C语言string.h
-
string 其他函数操作
[basic_string 类 | Microsoft Docs](
#include"iostream "
#include <string> //注意和string.h 差别
#include <cstring> //string.h和cstring
using namespace std;
void Creatstring()
{
string Str;
Str = "ILOVECANDC++";
cout << Str << endl;
}
void OperateString()
{
string Str1,Str2;
Str1 = "Im S1";
Str2 = "Im S2";
// additon
string Str3 = Str1 + Str2;
cout << Str1 << Str2 << endl << Str3 << endl;
//Compare String
if (Str1 > Str2)
cout << "Str1 is bigger than Str2" << endl;
else
cout << "Str2 is bigger than Str1" << endl;
// 采用下标法打印 string
string Str4="Imstring4";
for (int i = 0; i < 4; i++)
cout << Str4[i] << endl;
}
int main() {
Creatstring();
OperateString();
return 0;
}`
作业:
#include"iostream"
using namespace std;
void Test2Darry(int**& p, int row, int colomn) /*
我们要修改二级指针,所以传入二级指针的 引用*/
{
p = new int* [row]; // 二级指针 = 一级指针数组
for (int i = 0; i < row; i++)
{
p[i] = new int[colomn]; // 申请一级指针的内存
}
}
void print2Darry(int** p, int row, int colomn)
{ /* 传入二级指针,也就是二维数组数组名*/
for (int i = 0; i < row; i++)
{
for (int j = 0; j < colomn; j++)
{
p[i][j] = i * j;
cout << p[i][j] << "\t";// 数组名+下表方式输出
}cout << endl;
}
}
void deleted(int**& p, int row)
{
for (int i = 0; i < row; i++)
delete[] p[i];
delete []p ;
}
int main()
{
int** p = nullptr;//定义 将要指向二维数组的 指针
Test2Darry(p, 5, 4);
print2Darry(p, 5, 4);
deleted(p, 5);
while (1);
return 0;
}