C++学习笔记【02】——C++与C的区别2

这篇C++学习笔记详细介绍了C++与C的区别,包括新基本数据类型如bool,引用类型(左值引用与右值引用),结构体类型的特点,C++string的使用,以及C++中的自动类型推断(auto和decltype)和动态内存管理(new和delete)。
摘要由CSDN通过智能技术生成

目录

新基本数据类型

bool类型

引用类型

C++结构体类型

C++string类型

C++自动推断类型

C++动态内存申请


新基本数据类型

bool类型

  • 占用内存是1个字节

  • 打印出来的值是: 0或者1 非零值表示成立

  • 通常用的是false和true做初始化

void test1()
{
	bool bNum = 6;
	cout << bNum << endl;
}

引用类型

可以把引用类型理解为一个起别名的用法

  • 左值引用

    • 当做函数参数: 直接修改实参,防止拷贝本的产生

    • 当做返回值: 增加左值使用

void test2(int& x)
{
	x = 666;
}
int g_num = 100;
int& getValue()
{
	return g_num;
}
int main()
{
	int a = 1;
	int& b = a;
	b = 1001;
	cout << "a=" << a << endl;
	test2(b);
	cout << "a=" << a << endl;
	return 0;
}

 

  • const属性限定问题

    • 常引用(给常量起别名)

      不能被修改

C++对const属性更为严格,像函数传参需要用const来修饰,这样既可以传常量,又可以传变量

const int& num1 = 123;
//错位写法 int& num2 = 123;
//非常量引用的初始值必须为左值

 

右值引用 &&

  • 给右值起别名

  • 当做函数参数: 函数只能传入右值

    • 想要传入左值,使用move函数移动

void test3(int&& num)
{
	num++;
	cout << num << endl;
}
void test4(const int& num)
{
	cout << num << endl;
}
int main()
{
	test3(1);
	test4(233);
	//可以把左值变成右值
	int data = 100;
	int&& data2 = move(data);
	test3(move(data));
	return 0;
}

C++结构体类型

  • 类型名不在需要struct关键字了

  • C++结构体可以给成员直接赋初始值

  • C++结构体可以包含函数(可以结构体中声明,外面实现,类似于命名空间,结构体类型限定)

  • 其实C++结构体的处理就是按照类的方式处理

    • 用了构造函数时候C++结构体和C语言结构体处理方案是完全不同的

struct Student
{
	char name[20] = "张三";
	int num = 1001;
	void initData(const char* str, int num);
	void printdata()
	{
		cout << name << "\t" << num << endl;
	}
};
void Student::initData(const char* str, int num)
{
	strcpy_s(Student::name, 20, str);
	Student::num = num;
}
int main()
{
	Student a;
	/*cout << a.name << "\t" << a.num << endl;
	cout << "等效于" << endl;*/
	a.printdata();
	Student b = { "李四",1002 };
	/*cout << b.name << "\t" << b.num << endl;
	cout << "等效于" << endl;*/
	b.printdata();
	b.initData("王五", 1003);
	b.printdata();
	return 0;
}

 

C++string类型

C++string本身是一个类

  • 头文件: #include <string>

    • 注意点和cstring区别,cstring这个是C语言头文件

  • 没有用using namespace std ; string类型需要改为std::string

  • 掌握string常用方式

    • 创建方式

    • 基本操作(比较,连接)

int main()
{
	string str1;
	str1 = "张三";
	cout << str1 << endl;
	//赋值(长度没有限制)
	string str2 = "skshfasiunfakahibuebfabf";
	cout << "str2:";
	cout << str2 << endl;
	string str3(str2);
	string str4 = str3;
	cout << "str3:";
	cout << str3 << endl;
	cout << "str4:";
	cout << str4 << endl;
	//比较
	cout << (str3 == str4) << endl;
	//输入
	cout << "请输入:";
	string str5;
	cin >> str5;
	cout << "str5:";
	cout << str5 << endl;
	//连接
	string str6 = "法外狂徒" + str1;
	cout << "str6:";
	cout << str6 << endl;
	//等效于
	cout << str3.compare(str4) << endl;//比较
	cout << str1.append("果然是你") << endl;//连接
	//长度
    cout << "str6_length:";
	cout << str6.length() << endl;
	return 0;
}

 

 转C语言char* (C++中的string类型不能通过%s输出)

string str = "ILoveYou";
printf("%s\n", str.c_str());
printf("%s\n", str.data());

微软帮助文档:basic_string 类 | Microsoft Docs

C++自动推断类型

非常方便,懒人必备

  • auto类型

    • 类型自动推断: 一定要有赋值 所以不能单独定义变量

    • 没有赋值 推断不出来

struct A
{
	int num;
};
A g_A = { 100 };
struct A* createA()
{
	A* p = &g_A;
	return p;
}
int Max(int a, int b)
{
	return a > b ? a : b;
}
void print(int(*Max)(int, int), int a, int b)
{
	cout << Max(a, b) << endl;
}
void printFunc(int(*)(int, int), int, int)
{
	cout << "另一个函数" << endl;
}
int main()
{
	auto a = 1;
	auto pA = createA();
	cout << pA->num << endl;
	auto pFunc = print;	//类型为:void (*)(int(*)(int,int),int,int)
	pFunc(Max, 2, 3);
    //验证一下
	pFunc = printFunc;
	pFunc(Max, 2, 3);
	return 0;
}

 

注意,C++的NULL是不一样的

int* p = nullptr; 等效于C语言的NULL,即指向指针类型的空

  • decltype类型

    • 不需要赋值,可以推断根据类型

    • 像忘记了变量是什么类型,可以使用推断类型,一般用auto

    • 函数指针要取地址

decltype(123) num = 1;	//decltype(123) 表示一个int类型
decltype(&print) ppFunc;
ppFunc = printFunc;
ppFunc(Max, 1, 2);

 

C++动态内存申请

C++申请的内存是自由存储区的,C语言的堆区内存,所以C++类的对象内存不能用malloc申请

// IMAGE *

  • new申请内存

    • 申请单个变量内存

    • 申请一段内存

    • 申请内存可以手动初始化

    • 申请内存后可以再分配

  • delete释放内存

    • 释放单个变量内存:delete 指针名;

    • 释放一段变量内存: delete[] 指针名;

struct Student
{
	char name[20];
	int num;
};
void test1()
{
	int* pInt = new int;
	*pInt = 123;//访问方式和C语言一样的
	cout << pInt[0] << endl;
	pInt = nullptr;
	delete pInt;
    //初始化 
	int* pNum = new int(666);
	cout << pNum[0] << endl;
	Student* pS = new Student({ "张三",1001 });
	cout << pS->name << "\t" << pS->num << endl;
}
void test2()
{
	int* pArray = new int[4];	//int pArray[4];
	delete[] pArray;
	Student* pS = new Student[4];
	delete[] pS;
	pArray = nullptr;
	pS = nullptr;

	int* pNum = new int[4]{ 1,2,3,4 };
	for (int i = 0; i < 4; i++)
	{
		cout << pNum[i] << "\t";
	}
	cout << endl;
	delete[] pNum;
	pNum = nullptr;
}
void test3()
{
	char* pMem = new char[1024];
	//在原来上面拿个20字节存整数
	int* pInt = new(pMem + 0) int[5]{ 1,2,3,4,5 };
	//在原来上面拿出20个存字符
	char* pChar = new(pMem + 20) char[20]{ "ILoveyou" };
	//等效:char* pChar = new(pInt + 5) char[20]{"ILoveyou"};
	delete[] pMem;
	pMem = nullptr;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值