C++与C的区别(二)

本文详细介绍了C++中的结构体,包括结构体与C语言的区别,如函数成员的使用和直接访问数据成员。同时,深入探讨了动态内存管理,包括malloc、calloc、realloc和C++中的new、delete操作。此外,还讲解了string类型的基本操作,如创建、拷贝、赋值和比较,并对比了C++ string与C语言的字符串处理。最后,提到了内存池的概念以及如何使用内存池进行内存分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                      结构体区别

  • 类型上不再需要struct关键字,直接用结构体名即可
  • C++结构体中允许函数存在
  • 在结构体中声明,在结构体外实现,当然可以直接在结构体中实现
  • 结构体中函数访问数据,是可以直接访问
  • 学会调用,和数据成员方式时一样的
  •      对象(结构体变量).成员
  •      对象指针->成员
  •      (*对象指针).成员
  •       C++在没有写构造函数和权限限定的时候,用法和C语言的用法是一样
    #include <iostream>
    #include <string>
    using namespace std;
    struct MM 
    {
    	//protected:  不需要深究后续会讲
    	//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;
    }
    //结构体中的变量必须要通过结构体变量(结构体指针)访问
    //C++结构体中的函数访问属性,可以直接访问
    
    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();
    	MM array[3];
    	return 0;
    }

                                                              动态内存申请

  • C语言的动态内存申请
  •  malloc  不带初始化 ,calloc  带初始化,realloc   重新申请
  •  free 释放
  • C++的动态申请
  •  new(申请)和delete(释放)
  •  单个变量内存申请
  •  数组的动态申请
  •  结构体内存申请
  • #include <iostream>
    #include <cstring>
    using namespace std;
    void testOneMemory() 
    {
    	//申请不做初始化
    	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;
    	pInt = new int;
    	*pInt = 332;
    	cout << *pInt << endl;
    	delete pInt;
    	pInt = nullptr;
    
    	delete pChar;
    	pChar = nullptr;
    	delete pNum;
    	pNum = nullptr;
    }
    void testArrayMemory()
    {
    	//一维数组
    	//1.不带初始化
    	//长度可以是变量,只要值就可以
    	int* pInt = new int[3];		//等效产生了int pInt[3]的数组
    	//const char* pstr = new char[15];  //你 --->大老婆
    	//const char* pstr1 = pstr;         //朋友--->大老婆
    	//pstr = "ILoveyou";				  //你-->二老婆
    	char* pstr = new char[15];
    	strcpy_s(pstr, 15, "ILoveyou");
    	cout << pstr << endl;
    	//cout << pstr1 << 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]{ "ILoveyou" };
    	cout << str << endl;
    	delete[] str;
    	str = nullptr;
    	delete [] pInt;   //数组的释放 不需要大小
    	//释放只有两种形式 delete 指针   delete [] 指针
    	//delete [][] p 没有这种写法
    	pInt = nullptr;
    }
    struct MM 
    {
    	char* name;
    	int age;
    	//成员函数
    	void printMM() 
    	{
    		cout << name << "\t" << age << endl;
    	}
    };
    void testStructMemory()
    {
    	//new一个对象
    	int* p = new int(23);
    	//结构体只能用大括号
    	MM* pMM = new MM;
    	//结构体中指针,要做二次申请,才能strcpy,或者赋值
    	pMM->name = new char[20];
    	strcpy_s(pMM->name,20, "丽丝");
    	pMM->age = 188;
    	pMM->printMM();
    	//申请顺序和释放顺序是相反
    	delete[] pMM->name;
    	delete pMM;
    }
    
    int main() 
    {
    	//testOneMemory();
    	//testArrayMemory();
    	testStructMemory();
    	return 0;
    }

                                                                     内存池

  • 允许大家申请一段内存,共给程序使用,综合管理内存

  • #include <iostream>
    using namespace std;
    //允许大家申请一段内存,共给程序使用,综合管理内存
    //malloc 内存是在堆区
    //new 内存是自由存储区
    void testMemory() 
    {
    	char* memorySum = new char[1024];
    	//.......事情的处理,需要内存,所有内存源自于memorySum
    	//int* pNum = new(申请内存的开始位置) int[3]
    	int* pNum = new(memorySum) int[3]{ 1,2,3 };
    	//char* pstr = new(pNum + 3) char[20]{ "ILoveyou" };
    	//和下面这句话是等效的
    	char* pstr = new(memorySum + 12) char[20]{ "ILoveyou" };
    	for (int i = 0; i < 3; i++) 
    	{
    		cout << pNum[i] << " ";
    	}
    	cout << endl;
    	for (int i = 0; i < 3; i++)
    	{
    		cout << ((int *)memorySum)[i] << " ";
    	}
    	cout << endl << pstr << endl;
    	cout << (memorySum + 12) << endl;
    	delete[] memorySum;
    	memorySum = nullptr;
    }
    int main() 
    {
    	testMemory();
    	return 0;
    }

                                                                     string类型  

  • 只需要知道有这种用法即可,不需要大家深究为什么,因为string本身是一个类,需要讲完类的大部分知识,才能追究为什么这样做。自己也可以封装一个string 类型

  • string创建

    • 带初始化

    • 不带初始化

    • 通过另一个字符串创建

  • string基本操作

    • 拷贝

    • 赋值

    • 连接

    • 比较

  • C++string与C语言string.h

  • string 其他函数操作

    basic_string 类 | Microsoft Docs

    #include <string>   //注意和string.h区别
    #include <iostream>
    #include <cstring>	//string.h和cstring是一样
    #include <stdio.h>
    using namespace std;
    void createString() 
    {
    	//std::string  str;
    	string str1;
    	str1 = "ILoveyou";  //所以一般用string不会加const
    	cout << "First:" << str1 << endl;
    	const string cstr;
    	//cstr = "IMissyou";  错误,常属性不能修改
    
    	string str2("ILoveyou");
    	cout << str2 << endl;
    	string str3 = "IMissyou";   //喜欢这种方式
    	cout << str3 << endl;
    
    	string str4(str3);
    	cout << str4 << endl;
    
    	string str5 = str4;
    	cout << str5 << endl;
    	//一般没有长度限定,在你使用范围下
    	string str = "2333333333333333333333333333333333333333333333333333333333333";
    }
    
    void  operatorString() 
    {
    	string str1 = "one";
    	string str2 = "two";
    
    	string str3 = str2;
    	cout << str3 << endl;
    
    	//没有减法
    	string str4 = str1 + str2;
    	//等效: string str4=str1.append(str2);
    	//C++中尽量用string 不要用char*  ,可以用
    	//比较直接比较即可
    	//>  < != ==
    	//str1.compare(str2)  0 -1 1
    	if (str1 > str2)    //比较依旧按照char* 去比较
    	{
    		cout <<"大的 "<< str1 << endl;
    	}
    	else 
    	{
    		cout << "大的 " << str2 << endl;
    	}
    }
    void compareCAndCpp() 
    {
    	//C++中是一个自定义类型(类),目前当作结构体即可
    	//C++string 不能用到C语言的字符串处理函数
    	//C++如何转换为C语言的char* 
    	//c_str()  data()函数
    	string str1 = "ILoveyou";
    	//printf("%s", str1);
    	printf("%s\n", str1.c_str());
    	printf("%s\n", str1.data());
    	//outtextxy(int x,int y,char* str);
    
    	//直接把数字转换为相应的字符串
    	string str2 = to_string(1234);
    	//atoi
    	cout << str2 << endl;
    }
    void exOperator() 
    {
    	//采用下表法打印string
    	string str = "IMissyou";
    	//C++string中没有记录\0 
    	for (int i = 0; i < 8; i++) 
    	{
    		cout << str[i];
    	}
    	cout << endl;
    	//其他函数操作
    	//万金油函数
    	//empty()
    	//size();
    	string mystring = "IMissyou";
    	//cout << sizeof(mystring) << endl;   //28
    	//cout <<"容量:" <<mystring.capacity() << endl;
    	cout <<"mystring:"<< mystring.size() << endl;
    	string strEmpty;		
    	if (strEmpty.empty())				  //return length==0;
    	{
    		cout << "string为空" << endl;
    	}
    }
    void initString() 
    {
    	char* str = new char[15];
    	//做了自动扩增处理
    }
    int main() 
    {
    	//createString();
    	//operatorString();
    	//compareCAndCpp();
    	exOperator();
    	return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值