C++的基础学习

发布于:2023-01-22 ⋅ 阅读:(397) ⋅ 点赞:(0)

1.C++关键字

C++ 总计 63 个关键字, C 语言 32 个关键字

2.命名空间

    在 C/C++ 中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化 ,以 避免命名冲突或名字 污染 namespace 关键字的出现就是针对这种问题的。
#include <iostream>
#include <vector>
//标准库的东西都放到std
//using namespace std;
//1.项目中,尽量不要使用using namespace std;
//2.日常练习中用using namespace std;
//3.项目中可以指定命名空间访问+展开常用

//namespace bit
//{
//	int rand = 0;
//}
//
using namespace bit;
//
//int main()
//{
//	printf("%d\n", rand);
//
//	//去命名空间里面访问变量
//	//printf("%d\n", bit::rand);
//
//	return 0;
//}

using std::cout;
using std::endl;

int main()
{
	std::vector <int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

    cout << "hello bit" << endl;
	cout << "hello bit" << endl;
	cout << "hello bit" << endl;
	cout << "hello bit" << endl;


	return 0;
}

2.1 命名空间定义

    定义命名空间,需要使用到 namespace 关键字 ,后面跟 命名空间的名字 ,然 后接一对 {} 即可, {} 中即为命名空间的成员。
//1. 普通的命名空间
namespace N1 // N1为命名空间的名称
{
	// 命名空间中的内容,既可以定义变量,也可以定义函数
	int a;
	int Add(int left, int right)
	{
		return left + right;
	}
}
//2. 命名空间可以嵌套
namespace N2
{
	int a;
	int b;
	int Add(int left, int right)
	{
		return left + right;
	}

	namespace N3
	{
		int c;
		int d;
		int Sub(int left, int right)
		{
			return left - right;
		}
	}
}
//3. 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。
namespace N1
{
	int Mul(int left, int right)
	{
		return left * right;
	}
}
注意: 一个命名空间就定义了一个新的作用域 ,命名空间中的所有内容都局限于该命名空间中

2.2 命名空间使用

namespace N 
{
	int a = 10;
	int b = 20;
	int Add(int left, int right)
	{
		return left + right;
	}
	int Sub(int left, int right)
	{
		return left - right;
	}
}
int main()
{
	printf("%d\n", a); // 该语句编译出错,无法识别a
	return 0;
}
命名空间的使用有三种方式:
  • 加命名空间名称及作用域限定符
int main()
{
 printf("%d\n", N::a);
 return 0; 
}
  • 使用 using 将命名空间中成员引入
using N::b;
int main()
{
 printf("%d\n", N::a);
 printf("%d\n", b);
 return 0; 
}
  • 使用 using namespace 命名空间名称引入
using namespce N;
int main()
{
 printf("%d\n", N::a);
 printf("%d\n", b);
 Add(10, 20);
 return 0; 
}

3. C++输入&输出

3.1实现hello world

#include<iostream>
using namespace std;
int main()
{
 cout<<"Hello world!!!"<<endl;
 return 0; 
}

说明:

  • 使用 cout 标准输出 ( 控制台 ) cin 标准输入 ( 键盘 ) 时,必须 包含 < iostream > 头文件 以及 std 标准命名空间。
  • 使用 C++ 输入输出更方便,不需增加数据格式控制,比如:整形 --%d ,字符 --%c

3.2无数据格式控制 

#include <iostream>
using namespace std;
int main()
{
	int a;
	double b;
	char c;

	cin >> a;
	cin >> b >> c;

	cout << a << endl;
	cout << b << " " << c << endl;
	return 0;
}

3.3整体表达

#include <iostream>
#include <vector>

using namespace std;

int main()
{

	//相比c语言的特点是自动识别类型
	int i;
	double d;
	// >> 流提取
	cin >> i >> d;

	// << 流插入
	cout << i << endl;
	//endl本质就是换行的意思
	cout << d << endl;

	cout << "hello world" << endl;

	return 0;
}

4. 缺省参数

4.1 缺省参数概念

   缺省参数是 声明或定义函数时 为函数的 参数指定一个默认值 。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参。
#include <iostream>
#include <vector>

using namespace std;

//不传参数的时候,缺省参数起作用
//缺省参数
void Func( int a = 0 )
{
	cout << a << endl;
}

int main()
{
	Func(1);
	Func(2);
	Func(3);  //传参时,使用指定的实参
	Func();  //没有传参时,使用参数的默认值

	return 0;
}

  • 全缺省参数
#include <iostream>
#include <vector>

using namespace std;

//全缺省
void TestFunc(int a = 10, int b = 20, int c = 30)
{
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl << endl;
}

int main()
{
	TestFunc(); //10 20 30
	TestFunc(1);//从左往右给 1 20 30
	TestFunc(1,2); //1 2 30
	TestFunc(1,2,3); // 1 2 3


	return 0;
}
  • 半缺省参数
#include <iostream>
#include <vector>

using namespace std;

//半缺省
void TestFunc(int a, int b = 10, int c = 20) //必须从右往左连续缺省,不能间隔
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}

int main()
{
	TestFunc(1); //1 10 20
	TestFunc(1, 2); // 1 2 20
	TestFunc(1, 2, 3); // 1 2 3
	return 0;
}

5. 函数重载

5.1 函数重载概念

     函数重载 : 是函数的一种特殊情况, C++ 允许在 同一作用域中 声明几个功能类似 的同名函数 ,这些同名函数的 形参列表 ( 参数个数 或 类型 或 顺序 ) 必须不同 ,常用来处理实现功能类似数据类型不同的问题。
#include <iostream>
#include <vector>

using namespace std;

int Add(int left, int right)
{
	return left + right;
}

double Add(double left, double right)
{
	return left + right;
}

int main()
{
	cout << Add(1, 2) << endl;
	cout << Add(1.1, 2.2) << endl;
	return 0;
}
//参数顺序不同
void func(int i, char ch)
{
	cout << "void func(int i,char ch)" << endl;
}

void func(char ch, int i)
{
	cout << "void func(char ch,int i)" << endl;
}

int main()
{
	/*cout << Add(1, 2) << endl;
	cout << Add(1.1, 2.2) << endl;*/

	func(1, 'a');
	func('a', 1);
	return 0;
}
//返回值不同,不构成重载,short和int,调用时也无法区分
short Add(short left, short right)
{
	return left + right;
}

int Add(short left, short right) 
{
	return left + right;
}

5.2 名字修饰

     C 语言没办法支持重载,因为同名函数没办法区分。而 C++ 是通过函数修饰规则来区
分,只要参数不同,修饰出来的名字就不一样,就支持了重载。

5.3 extern “C”

    有时候在 C++ 工程中可能需要 将某些函数按照 C 的风格来编译 在函数前加 extern "C" ,意思是告诉编译器, 将该函数按照 C 语言规则来编译 。比如: tcmalloc google C++ 实现的一个项目,他提供 tcmallc() tcfree 两个接口来使用,但如果是C 项目就没办法使用,那么他就使用 extern “C” 来解决。
extern "C" int Add(int left, int right);
int main()
{
 Add(1,2);
 return 0; 
}

6. 引用

6.1 引用概念

    引用 不是新定义一个变量,而 是给已存在变量取了一个别名 ,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。
    类型& 引用变量名(对象名)=引用实体
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int a = 0;
	int& b = a;

	cout << &b << endl;//取地址
	cout << &a << endl;//取地址

	a++;
	b++;

	return 0;
}
注意: 引用类型 必须和引用 实体 同种类型

6.2 引用特性

  1. 引用在定义时必须初始化;
  2. 一个变量可以有多个引用
  3. 引用一旦引用一个实体,再不能引用其他实体
int main()
{
	int a = 1;
	//1.引用在定义时必须初始化
	//int& b;

	//2.一个变量可以有多个引用
	int& b = a;
	int& c = a;
	int& d = c;


	++a;

	//3.引用一旦引用一个实体,再不能引用其他实体
	int x = 10;
	b = x;//b是x的别名呢?还是x赋值给b呢?
	return 0;
}

6.3 常引用

void TestConstRef()
{
 const int a = 10;
 //int& ra = a; // 该语句编译时会出错,a为常量
 const int& ra = a;
 // int& b = 10; // 该语句编译时会出错,b为常量
 const int& b = 10;
 double d = 12.34;
 //int& rd = d; // 该语句编译时会出错,类型不同
 const int& rd = d;
 }

6.4 使用场景

    1. 做参数
//1.做参数 -- a、输出型参数;b、大对象传参,提高效率
void Swap(int& r1, int& r2)
{
	int tmp = r1;
	r1 = r2;
	r2 = tmp;
}

int main()
{
	int a = 0, b = 2;
	Swap(a, b);

	return 0;
}

7. 内联函数

7.1 概念

     以 inline 修饰 的函数叫做内联函数, 编译时 C++ 编译器会在 调用内联函数的地方展开 ,没有函数压栈的开销,内联函数提升程序运行的效率。
c -> 宏函数   c++ -> inline
宏的优点:a、代码可维护性;b、宏函数提高效率,减少栈帧建立。
宏的缺点:a、可读性差;b、没有类型安全检查;c、不方便调试、复杂。
#include <iostream>
using namespace std;

//ADD两个变量的宏函数
#define ADD(a,b) ((a)+(b))

//预处理--替换

int main()
{
	ADD(1, 2);//((1) + ((2))

	if (ADD(1, 2)) // if(((1) + (2)))
	{
	
	}

	ADD(1, 2) * 3;// ((1)+(2))*3
	int x = 1, y = 2;
	ADD(x | y, x & y);// ((x | y)+(x&y))

	return 0;
}

    c++中基本不再建议使用宏,尽量使用const、enum、inline去替代宏,inline几乎解决宏函数缺点,同时兼备其他的优点。

//inline 符合条件的情况,在调用地方展开
inline int Add(int a, int b)
{
	return a + b;
}


int main()
{
	cout << Add(1, 2) << endl;//((1) + ((2))

	if (Add(1, 2)) // if(((1) + (2)))
	{
		cout << Add(1, 2) << endl;
	}

	cout << Add(1, 2) * 3 << endl;// ((1)+(2))*3
	int x = 1, y = 2;
	Add(x | y, x & y);// ((x | y)+(x&y))

	return 0;
}

7.2 特性

  1. inline 是一种 以空间换时间 的做法,省去调用函数额开销。所以 代码很长 或者有 循环 / 递归 的函数不适宜使用作为内联函数。
  2. inline 对于编译器而言只是一个建议 ,编译器会自动优化,如果定义为 inline 的函数体内有循环 / 递归等等,编译器优化时会忽略掉内联。
  3. inline 不建议声明和定义分离,分离会导致链接错误。因为 inline 被展开,就没有函数地址了,链接就会找不到。

 8. auto关键字

8.1 auto简介

      C++11 中, auto 不再是一个存储类型指示符,而是作为一个新的类型 指示符来指示编译器, auto 声明的变量必须由编译器在编译时期推导而得
int TestAuto()
{
	return 10;
}
int main()
{
	int a = 10;
	auto b = a;
	auto c = 'a';
	auto d = TestAuto();

	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
	cout << typeid(d).name() << endl;

	//auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
	return 0;
}
//int
//char
//int
     注意: 使用 auto 定义变量时必须对其进行初始化,在编译阶段编译器需要根据初始化表达式来推导 auto 的实际类 。因此 auto 并非是一种 类型 的声明,而是一个类型声明时的 占位符 ,编译器在编译期会将 auto 替换为 变量实际的类型

8.2 auto的使用细则

  1. auto 与指针和引用结合起来使用 auto 声明指针类型时,用 auto auto* 没有任何区别,但用 auto 声明引用类型时则必须加 &。
  2. 在同一行定义多个变量 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对 第一个类型进行推导,然后用推导出来的类型定义其他变量
int main()
{
	int a[] = { 1,2,3,4,5,6 };
	for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
	{
		cout << a[i] << " ";
	}
	cout << endl;

	//范围for
	//自动依次取a的数据,赋值给e
	//自动迭代,自动判断结束
	for (auto e : a)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}

 8.3 auto不能推导的场景

  1. auto不能作为函数的参数
// 此处代码编译失败,auto不能作为形参类型,因为编译器无法对a的实际类型进行推导
void TestAuto(auto a)
{}
     2. auto 不能直接用来声明数组 
void TestAuto()
{
 int a[] = {1,2,3};
 auto b[] = {4,5,6};
}

9. 基于范围的for循环

9.1 范围for的语法

     for 循环后的括号由冒号 分为两部分:第一部分是范围内用于迭代的变量, 第二部分则表示被迭代的范围
    注意:与普通循环类似,可以用 continue 来结束本次循环,也可以用 break 来跳出整个循环

9.2 范围for的使用条件

    1. for 循环迭代的范围必须是确定的
     对于数组而言,就是数组中第一个元素和最后一个元素的范围 ;对于类而言,应该提供 begin end 的方法,begin end 就是 for 循环迭代的范围。
     注意:以下代码就有问题,因为 for 的范围不确定。
void TestFor(int array[])
{
 for(auto& e : array)
 cout<< e <<endl; 
}
    2. 迭代的对象要实现 ++ == 的操作

10. 指针空值nullptr

  • 在使用 nullptr 表示指针空值时,不需要包含头文件,因为 nullptr C++11 作为新关键字引入的
  • C++11 中, sizeof(nullptr) sizeof((void*)0) 所占的字节数相同。
  • 为了提高代码的健壮性,在后续表示指针空值时建议最好使用 nullptr
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到