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

请参见

参考

vector 类

标准模板库