當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。