目录
前言
1.第一个C++程序
首先,我们要了解C++的文件命名格式
C++兼容C语言绝大多数的语法,所以C语言实现的hello world依旧可以运行,C++中需要把定义文件代码后缀改为.cpp,vs编译器看到是.cpp就会调用C++编译器编译,linux下要用g++编译,不再是gcc。
// test.cpp
#include<stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
当然C++有自己的输入输出方式,严格说C++版本的hello world应该是这样写的。(后续会讲到)
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
2.命名空间
2.1概念理解
在C++中,命名空间是一种组织代码的结构,它允许你将一组具有唯一名称的实体(如类、函数和变量)封装在一起,从而避免了全局命名空间的名称冲突问题。命名空间对于大型项目和库尤其有用,因为它们可以减少名称碰撞的可能性,并使代码更易于管理。
在C++中定义命名空间非常简单,使用namespace关键字后跟命名空间的名字。
要访问命名空间中的成员,可以使用作用域解析运算符::
故接下来介绍namespace
2.2namespace 的价值
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进⾏本地化,以免命名 冲突或名字污染,namespace关键字的出现就是针对这种问题的。
c语言项目类似下面程序这样的命名冲突是普遍存在的问题,C++引⼊namespace就是为了更好的解决这样的问题
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int rand = 10;
int main()
{
// 编译报错:error C2365: “rand”: 重定义;以前的定义是“函数”
printf("%d\n", rand);
return 0;
}
C++使用namespace后:
#include <iostream>
#include <stdio.h>
namespace hu{
int rand = 10;
}
int main()
{
printf("%d\n", hu::rand);
return 0;
}
2.3 namespace的定义
在C++中,namespace
(命名空间)是C++语言的一个核心特性,不属于任何特定的库。
代码展示
#include <stdio.h>
//#include <stdio.h>
#include <stdlib.h>
// hu是命名空间的名字,⼀般开发中是⽤项⽬名字做命名空间名
namespace hu{
// 命名空间中可以定义变量/函数/类型
int rand = 10;
int add(int x, int y) {
return x + y;
}
//定义一个节点
struct Node
{
struct Node* next;
int val;
};
}
int main()
{
hu::Node* node = (hu::Node*)malloc(sizeof(hu::Node));
node->val = 1;
// 这⾥默认是访问的是全局的rand函数指针
printf("%p\n", rand);
// 这⾥指定hu命名空间中的rand
printf("%d\n", hu::rand);
printf("%d\n", hu::add(2, 3));
printf("%d\n", node->val);
return 0;
}
运行结果:
namespace只能定义在全局,当然还可以嵌套定义。
代码展示
namespace HL{
namespace H{
int rand = 1;
int Add(int left, int right)
{
return left + right;
}
}
namespace L{
int rand = 2;
int Add(int left, int right)
{
return (left + right) * 10;
}
}
}
int main()
{
printf("%d\n", HL::H::rand);
printf("%d\n", HL::L::rand);
printf("%d\n", HL::H::Add(1, 2));
printf("%d\n", HL::L::Add(1, 2));
return 0;
}
运行结果:
项目工程中多文件中定义的同名namespace会认为是一个namespace,不会冲突。
//头文件--function1.h
#include <stdio.h>
namespace MyNamespace {
void function1() {
printf("This is function1 in file1\n");
}
}
//头文件--function2.h
#include <stdio.h>
namespace MyNamespace {
void function2() {
printf("This is function2 in file2\n");
}
}
//测试文件
#include "function1.h"
#include "function2.h"
// 声明函数原型,以便在main函数中调用
int main() {
MyNamespace::function1();
MyNamespace::function2();
return 0;
}
3.命名空间的使用
编译查找一个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间里面去查找。所以下面程序会编译报错
#include<stdio.h>
namespace bite
{
int a = 0;
int b = 1;
}
int main()
{
// 编译报错:error C2065: “a”: 未声明的标识符
printf("%d\n", a);
return 0;
}
所以我们要使用命名空间中定义的变量/函数,有三种方式:
#include <stdio.h>
namespace N {
int a = 520;
int b = 1314;
}
int main()
{
// 指定命名空间访问
printf("%d\n", N::a);
// using将命名空间中某个成员展开
using N::b;
printf("%d\n", b);
return 0;
}
注意:
4.C++的输入输出
1.<iostream> 是 Input Output Stream 的缩写,是标准的输入、输出流库,定义了标准的输入、输
出对象。
2.std::cin 是 istream 类的对象,它主要面向窄字符(narrow characters (of type char))的标准输
入流。
3.std::cout 是 ostream 类的对象,它主要面向窄字符的标准输出流。
4.std::endl 是一个函数,流插入输出时,相当于插入一个换⾏字符加刷新缓冲区。
5.<<是流插入运算符,>>是流提取运算符。(C语⾔还用这两个运算符做位运算左移/右移)
6.使C++输入输出更方便,不需要像printf/scanf输入输出时那样,需要手动指定格式,C++的输入输出可以自动识别变量类型(本质是通过函数重载实现的),其实最重要的是C++的流能更好的支持自定义类型对象的输⼊输出。
7. IO流涉及类和对象,运算符重载、继承等很多面向对象的知识,这里简单认识⼀下C++ IO流的用法。
8.cout/cin/endl等都属于C++标准库,C++标准库都放在一个叫std(standard)的命名空间中,所以要
通过命名空间的使用方式去用他们。
9.一般日常练习中我们可以using namespace std,实际项目开发中不建议using namespace std。
10.这用我们没有包含<stdio.h>,也可以使用printf和scanf,在包含<iostream>间接包含了。vs系列
编译器是这样的,其他编译器可能会报错。
代码展示
#include <iostream>
using namespace std;
int main() {
int a = 10;
double b = 5.2;
char c = 'd';
cout << a << " " << b << " " << c << endl;
std::cout << a << " " << b << " " << c << std::endl;
scanf("%d %lf", &a, &b);
printf("%d %lf", a, b);
// 可以⾃动识别变量的类型
cin >> a;
cin >> b >> c;
cout << a << endl;
cout << b << " " << c << endl;
return 0;
}
扩展了解部分(提高效率)
#include<iostream>
using namespace std;
int main()
{
// 在io需求⽐较⾼的地⽅,如部分⼤量输⼊的竞赛题中,加上以下3⾏代码
// 可以提⾼C++IO效率
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}