C++与C语言02 结构体区别

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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值