在 Linux 中使用 C++ 的 STL(Standard Template Library,標準模板庫)非常簡單。首先確保你的系統已經安裝了支持 C++11 或更高版本的編譯器,如 GCC 或 Clang。然后按照以下步驟操作:
#include <iostream>
#include <vector>
#include <algorithm>
這些頭文件分別提供了輸入/輸出流、向量容器和算法庫的功能。
int main() {
// 使用 vector 容器
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 使用 STL 算法對容器進行排序
std::sort(numbers.begin(), numbers.end());
// 使用輸入/輸出流打印排序后的結果
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
-std=c++11 或更高版本的標志以啟用 C++11 或更高版本的功能。例如:g++ -std=c++11 your_file.cpp -o your_program
./your_program
這將輸出排序后的數字序列。
STL 提供了許多其他容器(如 list、set、map 等)和算法(如 find、count、transform 等)。你可以查閱 C++ 標準模板庫文檔以了解更多關于 STL 的信息。