# C++_std命名空間 所有 C++ 標準程式庫類型和函式都宣告於 std 命名空間中或 std 內的巢狀命名空間中。 ## C++中的std是甚麼? ```std::```是**命名空間指示符**,而C\++標準庫的函數或變數都是在std命名空間中定義的。 所以使用C\++標準庫中的函數或變數時都需要加上```std::``` 這時你可能會問**甚麼是C++標準庫呢?** * C\++標準庫,是類庫和函數的集合,由C\++標準委員會制定而成。 >[!Tip]小芝士🧀 >在C\++開發中要盡可能地使用標準庫完成,這可以大幅降低運算成本、提高編程效率,更能保持編程風格的一致性,提高可讀性。 ## 甚麼時候需要使用std? 如果你在調用標頭檔時使用了C++標準庫的內容 則需寫上```std::``` 如果使用非標準庫標頭檔,就無需加上```std::``` * 例:```#include<iostream.h>``` ```cp= #include<iostream.h> int main(){ cout<<"hello world"; cout<<endl;//這時你從iostream.h中調用的cout就無需加上std:: return 0; ``` 但是我們通常不使用非標準庫的標頭檔,這時想省去寫std::的方法就是```using namespace``` ```using namepace```能夠告訴compiler我們將使用```namspace```後的命名空間 * 例: ```using namespace std```=令compiler知道調用std命名空間的函數或變數,而不需在函數前加上std:: ```cpp= #include<iostream> using namespace std; int main(){ cout<<"hello,world"; cout<<endl;//這時你從iostream中調用的cout就無需加上std:: return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up