stl vector 函数_vector :: push_back()函数,以及C ++ STL中的示例

stl vector 函数

C ++ vector :: push_back()函数 (C++ vector::push_back() function)

vector::push_back() is a library function of "vector" header, it is used to insert/add an element at the end of the vector, it accepts an element of the same type and adds the given element at the end of the vector and increases the size of the vector.

vector :: push_back()“ vector”标头的库函数,用于在vector的末尾插入/添加元素,它接受相同类型的元素并在给定的末尾添加给定元素向量并增加向量的大小。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::push_back() function

vector :: push_back()函数的语法

    vector::push_back(value_type n);

Parameter(s): n – is an element to be added at the end of the vector.

参数: n –是要在向量末尾添加的元素。

Return value: void – In returns nothing.

返回值: void – In不返回任何内容。

Example:

例:

    Input:
    vector<int> v1;
    
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    Output:
    //if we print the values
    v1: 20 30 40 50   

C ++程序演示vector :: push_back()函数的示例 (C++ program to demonstrate example of vector::push_back() function)

//C++ STL program to demonstrate example of
//vector::push_back() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1;

    //inserting elements and printing size
    cout << "size of v1: " << v1.size() << endl;
    v1.push_back(10);
    cout << "size of v1: " << v1.size() << endl;
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);
    cout << "size of v1: " << v1.size() << endl;

    //printing all elements
    cout << "elements of vector v1..." << endl;
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

size of v1: 0
size of v1: 1
size of v1: 5
elements of vector v1...
10 20 30 40 50

Reference: C++ vector::push_back()

参考: C ++ vector :: push_back()

翻译自: https://www.includehelp.com/stl/vector-push_back-function-with-example.aspx

stl vector 函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值