| 特性 | <iostream.h> (舊式) | <iostream> (標準) |
|---|---|---|
| 標準狀態 | C++98 前慣例,**已棄用** | C++98 起正式標準 |
| 命名空間 | 全域命名空間 | std 命名空間 |
| 使用方式 | 直接用 cout | 需 std::cout 或 using namespace std; |
| 可移植性 | 編譯器專屬實現差異大 | 標準規範,跨平台一致 |
| 現代編譯器支援 | 部分保留為相容,**可能移除** | 全支援,強制標準 |
namespace std,並重新命名標頭檔(去 .h,C 庫加 c 前綴:<cstdio>)。.h 版本供舊代碼編譯,但不再維護更新。// C++ 標準庫
<iostream> ← <iostream.h>
<vector> ← (無舊版)
<string> ← <string.h> (注意:舊版 string.h 是 C 字串函數,非 std::string)
// C 標準庫 (C++ 版)
<cstdio> ← <stdio.h>
<cstring> ← <string.h>
<cstdlib> ← <stdlib.h>
<cmath> ← <math.h>
// ... 皆在 std 命名空間
// 推薦:显式限定(大型專案避免命名空間汙染)
#include <iostream>
int main() { std::cout << "Hello" << std::endl; }
// 可接受:實作檔/小程式引入整個 std
#include <iostream>
using namespace std;
int main() { cout << "Hello" << endl; }
// 禁止:頭檔中 using namespace std; (汙染所有引用者)
iostream.h 是 C 語言,iostream 是 C++」。using namespace std; 就能用 iostream.h」。using namespace std 無效且多餘。string.h 和 string 一樣」。string.h = C strcpy/strlen;cstring = 同功能入 std;string = std::basic_string 類別。新代碼務必使用 <iostream> 等無 .h 標準頭檔,配合 std:: 前綴或局部 using 声明。 理解命名空間機制與標準化歷史,能避免移植錯誤、符號衝突與舊版庫陷阱,是現代 C++ 開發者的基本素養。