回答
這意味着,從此代碼中的,當我們鍵入cout
我們意指std::cout
它注入在命名空間定義std
到當前命名空間中的cout
。我們在using namespace std
之上使用這個,因爲這更受控制;並不是每一個std
名字都會注入這個聲明。
但通常當我鍵入「cout」在一般情況下它工作正常。我從來沒有真正使用std :: cout,這就是爲什麼我想知道。發現了這個有趣的東西,不知道它的目的是什麼 – Masterminder
你之前是否使用過「using namespace std;'?因爲這會做同樣的事情(除了'endl'' swap'和其他東西噸) – jozefg
我總是這樣定義它... '使用命名空間標準;'但我想我現在看到它 – Masterminder
這是一個名稱空間聲明。允許您鍵入了cout
代替std::cout
和一般最好的using namespace std;
代替using
聲明引入的名稱cout
到全局命名空間爲std::cout
的代名詞。
它與「命名空間」概念有關。爲了避免名稱衝突(變量,類什麼的,它在不同的文件具有相同的名稱),你可以把你的代碼放到一個命名空間爲:
namespace exampleNS
{
class A { ... }
void aFunction (...){ ... }
}
當你在裏面的命名空間exampleNS
,你可以請參閱A
類,只使用名稱,但從外部您需要編寫exampleNS::A
。
如果您想要保存在名稱之前添加名稱空間的詳細信息(您肯定不會與當前名稱空間內的任何內容相沖突),那麼可以編寫該語句using
。
大多數標準庫實用程序都在namespace std
之內,例如變量cout
和cin
。在你的情況下,你的代碼不在命名空間std
中:你可以選擇在每次打印東西時寫入std::cout
,或者在開頭寫入using std::cout
,然後在代碼中使用它作爲cout
。
using A::B
凡A
是一個命名空間,意味着B
無障礙不需要的前綴,它的衍生物。請注意,這僅與放置它的範圍有關。如果放置在較低級別的作用域中,則該功能在外部作用域中不會有任何影響。
- 1. 關於std :: cin的澄清:
- 2. std :: string,wstring,u16/32string澄清
- 3. C++ - 最佳實踐:使用'的std :: cout` VS'的std :: cout`
- 4. std :: cout << x;和std :: cout << x << std :: endl;?
- 5. CString to std :: cout
- 6. std :: chrono和cout
- 7. 重定向std :: cout
- 8. std :: cout如何打印一個std :: string?
- 9. std :: cout和std :: wcout有什麼區別?
- 10. setbase(8)和std :: cout << std :: oct
- 11. 使用std :: unary_function清除std :: vector指針
- 12. 用定義替換std :: cout
- 13. 使用std :: ostream和std :: cout進行C++日誌記錄
- 14. 使用std :: cout的表格佈局
- 15. 從std :: cout或std :: ofstream(文件)獲取std :: ostream
- 16. C++概念和std :: cout
- 17. Xcode std :: cout輸出奇數
- 18. std :: cout將不打印
- 19. 螺紋安全std :: cout
- 20. Windows Phone原生std :: cout
- 21. 不能理解「std :: cout」
- 22. COUT怎樣的std ::工作
- 23. 新來的Xcode,不能使用cout,只有std :: cout的作品
- 24. 使用std :: lower_bound
- 25. 在std :: unique_ptr中使用std :: bind in lambda
- 26. 混淆使用std ::少和std ::有更大的std ::排序
- 27. 通過使用std :: get,std :: tuple_size,std :: tuple_element
- 28. 的std ::異步([](){的std :: COUT << 「你好」;})建立錯誤
- 29. 什麼是std :: cout << std :: cin做什麼?
- 30. 試圖在std :: cout和std :: ifstream之間切換
只是不要忘記rtfm,http://www.cplusplus.com/doc/tutorial/namespaces/ –