Day2:区别

结构体变量

#include<cstdio>
#include<iostream>
using namespace std;
struct GieGie 
{
	int age;
	char name[20];
	void print()
	{
		cout << name << "\t" << age << endl;
	}
	void printDate();
	int& changeage()
	{
		return age;
	}
};
void GieGie::printDate()//加上结构体限定,告诉人们来自哪
{
	cout<< name << "\t" << age << endl;
}
int main()
{
	GieGie wbm = { 203,"王不辉" };
	wbm.print();
	wbm.printDate();
	GieGie* p = (&wbm);
	p->print();
	p->changeage() = 9;
	p->print();
	GieGie arr[3];
	return 0;
}

 char型指针new一个arr  不可以用一下这种方式初始化

这仅仅只是改变了指针的指向,原先开辟的一段内存的内容并没有改变。

(正确的操作方式!) 

用字符串拷贝函数(pstr,15,串);

void test2()
{
	char* pChar = new char[20];
	strcpy_s(pChar, 15, "loveisgone");
	cout << pChar << endl;
}

一系列的数组初始化直接带一个{ }就行

void test2()
{
	int* pNum = new int[3]{ 1,2,3 };
	for (int i = 0; i < 3; i++)
	{
		cout << pNum[i] << endl;
	}
    delete []pNum;//[]就是告诉人们释放的类型与单个开辟的内存不同
}

 对字符串的初始化的操作,下面这两个方法是等效的。


void test2()
{
	char* str = new char[20]{ '2','a','\0'};
	char* str2 = new char[20]{ "2a" };
	cout << str << endl;
	cout << str2 << endl;
	delete[]str;
	str = nullptr;
	delete[]str2;
	str2 = nullptr;
}

有关结构体的 内存开辟:(照葫芦画瓢)

struct GieGie 
{
	char* name;
	int age;
};

这边在初始化时会报错,类型不一致(一个字符串是const char*)

 正确写法:

struct GieGie 
{
	const char* name;
	int age;
	void print()
	{
		cout << name << "\t" << age << endl;
	}
};
void test2()
{
	int* pNum = new int(2);//依葫芦画瓢
	GieGie* pGie = new GieGie{ "王饱明",20 };
	pGie->print();
}

 注:实际上在使用的时候不建议

这样初始化,因为如果要对name做后续修改的话的,const类型不好修改。

于是我们把结构体中const去掉,赋值。但仍然出了问题?why?

结构体中指针未初始化,地址不知道在哪,所以在strcpy的时候,编译器很蒙蔽,不知道你要在哪去操作这20个空间。

因此我们结构体中指针,要做二次申请,才能strcpy,或者赋值

struct GieGie 
{
	char* name;
	int age;
	void print()
	{
		cout << name << "\t" << age << endl;
	}
};
void test2()
{
	GieGie* pGie = new GieGie;
	pGie->name= new char[20];//关键!!!!!
	strcpy_s(pGie->name, 20, "wangbaobao");
	pGie->age = 18;
	pGie->print();
}

注意倒数第四行的关键开辟内存! 

	delete[]pGie->name;
	delete pGie;//后面放指针就行

我们也分两批去释放掉内存(先申请的后释放!!!!!) 

内存池

void testMemoryPole()
{
	//方便统一释放内存
	char* MemorySum = new char[1000];
	int* pNum = new(MemorySum)int[3]{ 10,11,12 };
	char* pChar = new(MemorySum+3*4)char[5]{"love"};
	//等效于   原理就是指针的偏移
	//char* pNum = new(pNum + 3)char[5]{ "love" };
	cout << pNum[1] << endl << pChar << endl;
	delete[]MemorySum;
	MemorySum = nullptr;
}

 

   2种写法等效,就是指针的应用

string类型(注意头文件是<string>bushi<string.h>)

第一种类型:加const则不能再去修改了

不初始化

string str1;
	str1 = "vavasd";
	cout << str1 << endl;
	const string str2;
	str2 = "1213";

所以我们用string 的时候一般不加const 

或者我们可以用()顺带着初始化,当然=用的更多一点!

string str1 ("vavasd");
	cout << str1 << endl;
string str2 = "cansdsad";//更常用一点吧
	cout << str2 << endl;

我们也可以通过其他的字符串来给新的字符串做初始化! 

string str3(str2);
string str4 = str3;

字符串中基本操作:

①赋值、拷贝:

string str1=str2;//直接赋值。

 ②连接:直接用加号

	string str1 ("vavasd");
	string str2 = "cansdsad";
	string str3 = str1 + str2;
    //等效于str3=str1.append(str2);
	cout << str3 << endl;

③ 比较大小 直接用关系运算符> < ==   !=

string str1 ("vavasd");
	string str2 = "vansdsad";
	if (str1 < str2)//等效于str1.compare(str2);
		cout << 1 << endl;
	else cout << 2;

与C++的区别:是一个类,暂时可以当做一个结构体,如何变成一个char*的类型

方法采用c_str()或者是data()函数。

(%s对应的是一个char*类型)。

string str2="abc";
printf("%s",str2.data());
//或者
printf("%s",str2.c_str());

骚操作to_string()把一个数字转换成对应的字符串

string str1 = to_string(1234);
	cout << str1 << endl;

C++中的string类型未存储'\0'

下标法来打印一个字符串

string str = "ce4 is coming";
	for (int i = 0; i < 14; i++)
	{
		cout << str[i];
	}

 其他函数操作:

string str2 = "enjoy the game";
	cout << str2.size() << endl;
	cout << str2.capacity() << endl;//原型是一个new char[k]k是一个数,为容量,可根据用户输入再扩充
	cout << str2.empty() << endl;//return lenght==0;为1则空

 一三用的更多点,得到string类型的长度(无终止符)和判断是否是空str

其他问答:

①结构体内 char * 为啥下面复制字符串的时候要用 const

这个跟赋值的值有关系,因为C++对const要求更严格,=两边类型必须一致

下面两行的操作 :第一行就是将常量区的内容给到栈变量区,那么此时指针变量再去指向一个变量是可以的。(如下图)

②回顾一下malloc calloc realloc

③指针偏移问题char*p   p+12

等效于 int *p    的  p+3;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Ocean__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值