在 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 的信息。