C语言的第一个程序
C++兼容C语⾔绝⼤多数的语法,所以C语⾔实现的helloworld依旧可以运⾏,C++中需要把定义⽂件 代码后缀改为.cpp,vs编译器看到是.cpp就会调⽤C++编译器编译,linux下要⽤g++编译,不再是gcc
// test.cpp #include<stdio.h> int main() { printf("hello world\n"); return 0; }
当然C++有⼀套⾃⼰的输⼊输出,严格说C++版本的helloworld应该是这样写的。
// test.cpp // 这⾥的std cout等看不懂没关系,下⾯我会依次讲解 #include<iostream> using namespace std; int main() { cout << "hello world\n" << endl; return 0; }
命名空间
namespace的价值
在C/C++中,变量、函数和后⾯要学到的类都是⼤量存在的,这些变量、函数和类的名称将都存在于全局作⽤域中,可能会导致很多冲突。使⽤命名空间的⽬的是对标识符的名称进⾏本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。
c语⾔项⽬类似下⾯程序这样的命名冲突是普遍存在的问题,C++引⼊namespace就是为了更好的解决这样的问题
#include <stdio.h> #include <stdlib.h> int rand = 10; int main() { // 编译报错:error C2365: “rand”: 重定义;以前的定义是“函数” printf("%d\n", rand); return 0; }
全局变量rand与stdlib的rand函数存在命名冲突存在命名冲突导致编译报错
namespace的定义
一、定义命名空间,需要使⽤到namespace关键字,后⾯跟命名空间的名字,然后接⼀对{}即可,{}中即为命名空间的成员。命名空间中可以定义变量/函数/类型等。
// zzy是命名空间的名字,⼀般开发中是⽤项⽬名字做命名空间名。 namespace zzy { / }
namespace zzy { // 命名空间中可以定义变量/函数/类型 int rand = 10; int Add(int left, int right) { return left + right; } struct Node { struct Node* next; int val; }; }
二、namespace本质是定义出⼀个域,这个域跟全局域各⾃独⽴,不同的域可以定义同名变量,所以下⾯的rand不在冲突了。
#include <stdio.h> #include <stdlib.h> namespace bit { int rand = 10; } int main() { // 这⾥默认是访问的是全局的rand函数指针 printf("%p\n", rand); // 这⾥指定zzy命名空间中的rand printf("%d\n", zzy::rand); //::域作用限定符 return 0; }
三、C++中域有函数局部域,全局域,命名空间域,类域;域影响的是编译时语法查找⼀个变量/函数/ 类型出处(声明或定义)的逻辑,有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的⽣命周期,命名空间域和类域不影响变量⽣命周期。
局部域 和全局域出了作用域就会销毁,命名空间域中的变量为全局变量,因名字隔离不会与全局域冲突
#include <stdio.h> #include <stdlib.h> #include <string.h> int x = 0; namespace zzy { int x = 1; } void func() { int x = 2;//局部域只能在当前局部访问 } int main() { int x = 3; printf("%d\n", x);//访问当前局部域 printf("%d\n", zzy::x);//访问命名空间域 printf("%d\n", ::x);//访问全局域 return 0; }
四、namespace只能定义在全局,当然他还可以嵌套定义。
#include <stdio.h> #include <stdlib.h> #include <string.h> namespace 602 { // zzy namespace zzy { int rand = 1; int Add(int left, int right) { return left + right; } } // lkk namespace lkk { int rand = 2; int Add(int left, int right) { return (left + right) * 10; } } } int main() { printf("%d\n", bit::zzy::rand); printf("%d\n", bit::lkk::rand); printf("%d\n", bit::zzy::Add(1, 2)); printf("%d\n", bit::lkk::Add(1, 2)); return 0; }
五、项⽬⼯程中多⽂件中定义的同名namespace会认为是⼀个namespace,不会冲突。
// 多⽂件中可以定义同名namespace,他们会默认合并到⼀起,就像同⼀个namespace⼀样 // Stack.h #pragma once #include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include<assert.h> namespace bit { typedef int STDataType; typedef struct Stack { STDataType* a; int top; int capacity; }ST; void STInit(ST* ps, int n); void STDestroy(ST* ps); void STPush(ST* ps, STDataType x); void STPop(ST* ps); STDataType STTop(ST* ps); int STSize(ST* ps); bool STEmpty(ST* ps); } // Stack.cpp #include"Stack.h" namespace bit { void STInit(ST* ps, int n) { assert(ps); ps->a = (STDataType*)malloc(n * sizeof(STDataType)); ps->top = 0; ps->capacity = n; } // 栈顶 void STPush(ST* ps, STDataType x) { assert(ps); // 满了, 扩容 if (ps->top == ps->capacity) { printf("扩容\n"); int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType)); if (tmp == NULL) { perror("realloc fail"); return; } ps->a = tmp; ps->capacity = newcapacity; } ps->a[ps->top] = x; ps->top++; } //... } // Queue.h #pragma once #include<stdlib.h> #include<stdbool.h> #include<assert.h> namespace bit { typedef int QDataType; typedef struct QueueNode { int val; struct QueueNode* next; }QNode; typedef struct Queue { QNode* phead; QNode* ptail; int size; }Queue; void QueueInit(Queue* pq); void QueueDestroy(Queue* pq); // ⼊队列 void QueuePush(Queue* pq, QDataType x); // 出队列 void QueuePop(Queue* pq); QDataType QueueFront(Queue* pq); QDataType QueueBack(Queue* pq); bool QueueEmpty(Queue* pq); int QueueSize(Queue* pq); } // Queue.cpp #include"Queue.h" namespace bit { void QueueInit(Queue* pq) { assert(pq); pq->phead = NULL; pq->ptail = NULL; pq->size = 0; } // ... } // test.cpp #include"Queue.h" #include"Stack.h" // 全局定义了⼀份单独的Stack typedef struct Stack { int a[10]; int top; }ST; void STInit(ST* ps) {} void STPush(ST* ps, int x) {} int main() { // 调⽤全局的 ST st1; STInit(&st1); STPush(&st1, 1); STPush(&st1, 2); printf("%d\n", sizeof(st1)); // 调⽤bit namespace的 bit::ST st2; printf("%d\n", sizeof(st2)); bit::STInit(&st2); bit::STPush(&st2, 1); bit::STPush(&st2, 2); return 0; }
六、C++标准库都放在⼀个叫std(standard)的命名空间中。
命名空间的使用
编译查找⼀个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间⾥⾯去查找。所以下⾯程序会编译报错。
#include<stdio.h> namespace zzy { int a = 0; int b = 1; } int main() { // 编译报错:error C2065: “a”: 未声明的标识符 printf("%d\n", a); return 0; }
所以我们要使⽤命名空间中定义的变量/函数,有三种⽅式:
- 指定命名空间访问,项⽬中推荐这种⽅式。
#include<stdio.h> namespace zzy { int a = 0; int b = 1; } // 指定命名空间访问 int main() { printf("%d\n", zzy::a); return 0; }
- using将命名空间中某个成员展开,项⽬中经常访问的不存在冲突的成员推荐这种⽅式。
#include<stdio.h> namespace zzy { int a = 0; int b = 1; } // using将命名空间中某个成员展开 using zzy::b; int main() { printf("%d\n", zzy::a); printf("%d\n", b); return 0; }
- 展开命名空间中全部成员,项⽬不推荐,冲突⻛险很⼤,⽇常⼩练习程序为了⽅便推荐使⽤。
#include<stdio.h> namespace zzy { int a = 0; int b = 1; } // 展开命名空间中全部成员 using namespce zzy; int main() { printf("%d\n", a); printf("%d\n", b); return 0; }
C++输⼊&输出
- <iostream>是 Input Output Stream 的缩写,是标准的输⼊、输出流库,定义了标准的输⼊、输 出对象。