目录
1.命名空间
在写代码时,有许多库中的变量、函数和类的名称(如·cin)会与我们定义的变量名冲突。
如果我们想定义一个名为int的变量,我们应该怎么办呢?
C++的命名空间就解决了这个问题。
命名空间(命名空间域),是用于避免命名冲突和名字污染。
1.1命名空间的定义
定义命名空间,要用到namespace关键字。
用法:namespace + 命名空间的名字 + { 命名空间成员 }
示例:我们可以这样创建名为cout的整型变量
#include<iostream>
namespace test
{
int cout = 1;
}
int main(){
std::cout << test::cout << std::endl;
test::cout++;
std::cout << test::cout << std::endl;
return 0;
}
可见,在命名空间的作用下,test::cout 和 std::cout 被区分开来。
1.2命名空间的使用
方式一:加命名空间及作用域限定符
#include<iostream>
namespace test
{
int a = 1;
}
int main() {
printf("%d\n", test::a);
return 0;
}
方式二:使用using将命名空间种某个成员引入
using test::a;
int main() {
printf("%d\n", a);
return 0;
}
方式三:使用using namespace命名空间名称引入
using namespace test;
int main() {
printf("%d\n", a);
return 0;
}
2.C++输入&输出
输入cin&输出cout,相当于c语言中的scanf和printf.
>>是提取流运算符,<<是插入流运算符。endl是换行。
重点:与c语言不同,cin&cout可以自动识别格式。
示例1:
cout<<"hello world!"<<endl
cout<<"hello world!"<<'\n'
示例2:
int main() {
int n;
cin >> n;
double* a = (double*)malloc(sizeof(double) * n);
if (a == NULL) {
perror("malloc fail");
exit(-1);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cout << a[i] << endl;
}
return 0;
}
示例二运行结果:
3.缺省参数
3.1缺省参数概念
在c语言中,调用函数必须要传参。
在c++中,声明或定义函数时可以给函数的参数指定一个缺省值;
如果调用函数时不传参数,则启用函数形参的缺省值;否则用指定的实参。
void Print(int a = 0) {
cout << a << endl;
}
int main() {
Print();
Print(10);
return 0;
}
3.2缺省参数分类
a.全缺省参数
void Func(int a = 10, int b = 20, int c = 30) {
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
b.半缺省参数
void Func(int a, int b = 10, int c = 20) {
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
注意1:给缺省值时,从左到右按顺序给,不可以Func(int a=0, int b, int c=10);
注意2:使用缺省值时,一定从左到右连续使用,不可以Func( , 2 , )这样调用。
注意3:缺省参数不可以在声明和定义中同时出现,涉及声明和定义分离的时候,在声明时给。
4.函数重载
C语言中不允许同名函数的存在,C++的函数重载解决了这个问题。
C++允许在同一作用域声明几个功能类似的函数:这些函数的形参列表(参数个数 / 参数类型 /类型顺序)不同,常用于处理功能类似但数据类型不同的问题。
#include<iostream>
using namespace std;
// 1、参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
}
// 2、参数个数不同
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
// 3、参数类型顺序不同
void f(int a, char b)
{
cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
cout << "f(char b, int a)" << endl;
}
5.引用
5.1引用定义
引用不是定义一个新的变量,而是给已存在的变量取了一个别名,二者共用同一块空间。
int main() {
int a = 10;
int& r = a; // <===定义引用类型
return 0;
}
5.2引用特性
1.引用在定义时必须初始化。
2.一个变量可以有多个引用(别名)。
3.一个引用只能引用一个实体。(引用不可改变指向)
5.3应用场景
1.引用做参数
void swap(int& x, int& y) {
int tmp = x;
x = y;
y = tmp;
}
2.做返回值。
6.内联函数
以inline修饰的函数叫做内联函数,编译时c++会在调用内联函数的地方展开。
内联函数示例:(相当于C语言中的宏函数)
inline int Add(int x, int y) {
int r = x + y;
return r;
}
int main() {
int ret = Add(1, 2);
cout << ret << endl;
}
注意:inline内联函数是一种以空间换时间的做法,不建议声明和定义分离。
7.auto关键字
随着程序的复杂,程序中用到的类型也越来越复杂;
当类型难以拼写,含义不明确导致容易出错;
我们可以用auto关键字让编译器自动推导类型。(auto右边必须给值进行初始化)
示例1:
示例2:(实际应用)
#include<vector>
#include<string>
int main(){
vector<string> v;
//vector<string>::iterator it = v.begin();
auto it = v.begin();
}
注意:auto不能做参数和返回值;auto不能声明数组。
8.范围for循环
如果我们想遍历一个数组:
在c++中有了范围for----可以自动依次取数组中的数据的值赋值给e对象,自动判断结束。
但是这里的auto e只是数组中数据的临时拷贝,如果我们相对数组中数据进行修改呢?
需要对e传引用:auto& e
int array[] = { 1,2,3,4,5,6,4 };
for (auto& e : array) {
e *= 2;
cout << e << " ";
}
注意:数组传参时函数中不能用范围for,因为范围for的迭代范围必须是确定的。
9.空指针nullptr
1.在使用nullptr表示指针空值时,不需要包含头文件
2.为了提高代码的健壮性,在后续表示指针空值时建议最好使用nullptr。