目錄
前言
1.第一個C++程序
2.命名空間
2.1概念理解
2.2namespace 的價值
2.3 namespace的定義
3.命名空間的使用
4.C++的輸入輸出
結束語
前言
本節我們將正式進入C++基礎的學習,話不多說,直接上貨!!!
1.第一個C++程序
// test.cpp
#include<stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
#include <iostream>
using namespace std;
int main() {cout << "hello world" << endl;return 0;
}
2.命名空間
2.1概念理解
在計算機科學中,命名空間是一種將一組符號名稱(如變量名、函數名、類名等)綁定到特定作用域的上下文的方法,以避免名稱沖突。在不同的編程語言中,命名空間可能以不同的形式存在,但基本概念是相似的。
在C++中,命名空間是一種組織代碼的結構,它允許你將一組具有唯一名稱的實體(如類、函數和變量)封裝在一起,從而避免了全局命名空間的名稱沖突問題。命名空間對于大型項目和庫尤其有用,因為它們可以減少名稱碰撞的可能性,并使代碼更易于管理。
在C++中定義命名空間非常簡單,使用namespace關鍵字后跟命名空間的名字。
要訪問命名空間中的成員,可以使用作用域解析運算符::
2.2namespace 的價值
#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++語言的一個核心特性,不屬于任何特定的庫。
1.定義命名空間,需要使用到namespace關鍵字,后面跟命名空間的名字,然后接?對{}即可,{}中即為命名空間的成員。命名空間中可以定義變量/函數/類型等。2.namespace本質是定義出?個域,這個域跟全局域各自獨立,不同的域可以定義同名變量,所以rand不在沖突了。3.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命名空間中的randprintf("%d\n", hu::rand);printf("%d\n", hu::add(2, 3));printf("%d\n", node->val);return 0;
}

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;
}

//頭文件--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;
}
? 指定命名空間訪問,項目中推薦這種方式。? using將命名空間中某個成員展開,項目中經常訪問的不存在沖突的成員推薦這種方式。? 展開命名空間中全部成員,項目不推薦,沖突風險很大,日常小練習程序為了方便推薦使用。
#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;
}
C++標準庫都放在?個叫std(standard)的命名空間中標準庫命名空間: C++標準庫中的所有功能都定義在std命名空間中。你可以使用std::前綴來訪問標準庫中的成員,或者使用using聲明或指令來引入特定的成員。在使用命名空間時,應該考慮到代碼的清晰性和可維護性。雖然using指令可以簡化代碼,但過度使用可能會導致名稱沖突和代碼的不可預測性。因此,最好是在局部作用域或函數內部使用using聲明,而不是在全局作用域或頭文件中使用using namespace指令。
4.C++的輸入輸出
代碼展示
#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;
}
結束語
本節內容新知識不是很多,著重講了namespace的用法,下節內容將帶來更加有趣的C++知識,歡迎大家的捧場!!!
最后感謝各位友友的支持,友友們動個手指點個贊,留個評論吧!!!