cout 定義在 <iostream> 頭文件中,屬於 std 命名空間。直接使用需 同時 滿足:
#include <iostream>using namespace std; 或前綴 std::cout#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
| 寫法 | 優點 | 缺點 |
|---|---|---|
using namespace std; | 簡潔 | 大型專案易命名衝突 |
using std::cout; | 精確引入 | 需逐個聲明 |
std::cout | 零污染、明確 | 代碼稍長 |
using namespace std; 最省事。return 0; 習慣 (C++ 標準要求 main 返回 int)。