在Ubuntu中,C++的STL(Standard Template Library)是默认包含在编译器中的。要在C++程序中使用STL,你需要遵循以下步骤:
#include <iostream>
#include <vector>
using namespace std;
现在你可以直接使用STL组件,而不需要加上std::前缀。
int main() {
vector<int> numbers;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
for (int num : numbers) {
cout << num << endl;
}
return 0;
}
g++ -o my_program my_program.cpp
这将生成一个名为my_program的可执行文件。要运行此程序,请在终端中输入以下命令:
./my_program
这将输出vector中的元素。
注意:在实际编程中,为了避免潜在的命名冲突,建议仅在需要的地方使用using namespace std;,而不是在整个文件中使用。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 如何解决Ubuntu静态IP不生效