在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;,而不是在整個文件中使用。