vector::push_back
將元素加入至向量結尾。
void push_back(const T& Val); void push_back(T&& Val);
參數
- Val
要指派給加入到向量結尾之元素的值。
範例
// compile with: /EHsc /W4
#include <vector>
#include <iostream>
using namespace std;
template <typename T> void print_elem(const T& t) {
cout << "(" << t << ") ";
}
template <typename T> void print_collection(const T& t) {
cout << " " << t.size() << " elements: ";
for (const auto& p : t) {
print_elem(p);
}
cout << endl;
}
int main()
{
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(10 + i);
}
cout << "vector data: " << endl;
print_collection(v);
// pop_back() until it's empty, printing the last element as we go
while (v.begin() != v.end()) {
cout << "v.back(): "; print_elem(v.back()); cout << endl;
v.pop_back();
}
}
輸出
需求
標頭:<vector>
命名空間: std