当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ vector::insert()用法及代码示例


C++ vector::insert() 函数

vector::insert() 是 "vector" 头文件的库函数,用于在向量中插入元素,它接受一个元素、一组具有默认值的元素或来自其他容器的其他值,并从指定的迭代器位置插入向量中。

注意:要使用矢量,请包括<vector>标题。

vector::insert() 函数的语法

    //inserting an element
    vector::insert(iterator position, value);

    //inserting multiple elements 
    vector::insert(iterator position, size, default_value);

    //inserting elements from other containers 
    vector::insert(iterator position, iterator start_position, iterator end_position);

参数:

在插入元素的情况下: iterator position, value——iterator position是使用要添加元素的向量的迭代器的索引,value是一个元素。

在插入多个元素的情况下: iterator position, size, default_value——iterator position是使用要添加元素的向量的迭代器的索引,size是要添加的元素数和default_value是将插入向量中的值。

在从其他容器插入元素的情况下: iterator position, iterator start_position, iterator end_position——iterator position是使用要添加元素的向量的迭代器的索引,start_position, iterator end_position是另一个容器的迭代器,其值将被插入到当前向量中。

返回值:

在插入元素的情况下,它返回一个iterator指向第一个新添加的元素,在其他情况下,它返回void

例:

    Input:
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50 };
    //array
    int x[] = { 1, 2, 3, 4, 5 };
    

    //inserting an element
    v1.insert(v1.begin() + 2, 100); //inserts 100 at 2nd index
    
    //inserting multiple elements with default value
    //inserts 5 elements (99) from 2nd index
	v1.insert(v1.begin() + 2, 5, 99);     
    //inserting array (other containers elements)
    
    //inserts 3 elements (from array) from 1st index
	v1.insert(v1.begin() + 1, x + 0, x + 3); 
    
    Output:
    v1:10 1 2 3 20 99 99 99 99 99 100 30 40 50

C++程序演示vector::insert()函数的例子

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

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

int main()
{
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50 };
    //array
    int x[] = { 1, 2, 3, 4, 5 };

    //printing elements
    cout << "before inserting the elements..." << endl;
    cout << "size of v1:" << v1.size() << endl;
    cout << "v1:";
    for (int x:v1)
        cout << x << " ";
    cout << endl;

    //inserting an element
    v1.insert(v1.begin() + 2, 100); //inserts 100 at 2nd index

    //printing elements
    cout << "after inserting an element..." << endl;
    cout << "size of v1:" << v1.size() << endl;
    cout << "v1:";
    for (int x:v1)
        cout << x << " ";
    cout << endl;

    //inserting multiple elements with default value
    //inserts 5 elements (99) from 2nd index
    v1.insert(v1.begin() + 2, 5, 99);

    //printing elements
    cout << "after inserting multiple elements..." << endl;
    cout << "size of v1:" << v1.size() << endl;
    cout << "v1:";
    for (int x:v1)
        cout << x << " ";
    cout << endl;

    //inserting array (other containers elements)
    //inserts 3 elements (from array) from 1st index
    v1.insert(v1.begin() + 1, x + 0, x + 3);

    //printing elements
    cout << "after inserting multiple elements..." << endl;
    cout << "size of v1:" << v1.size() << endl;
    cout << "v1:";
    for (int x:v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

输出

before inserting the elements... 
size of v1:5
v1:10 20 30 40 50 
after inserting an element...
size of v1:6
v1:10 20 100 30 40 50 
after inserting multiple elements... 
size of v1:11 
v1:10 20 99 99 99 99 99 100 30 40 50
after inserting multiple elements... 
size of v1:14 
v1:10 1 2 3 20 99 99 99 99 99 100 30 40 50

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



相关用法


注:本文由纯净天空筛选整理自 vector::insert() function with example in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。