C++ STL中的向量插入函数

std::vector::insert() 是C++ STL中的一个内置函数,它在指定位置之前在元素上插入新元素,有效地通过插入的元素数量来增加容器大小。

null
  1. 语法:
    vector_name.insert (position, val)
    

    参数: 该函数接受以下两个参数:

    • 位置—— 它指定指向插入位置的迭代器。
    • 瓦尔—— 它指定要插入的值。

    返回值: 该函数返回一个迭代器,该迭代器指向新插入的元素。

    例1: 下面的程序说明了在前面插入新元素的上述功能。

    // Program below illustrates the
    // vector::insert() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    // initialising the vector
    vector< int > vec = { 10, 20, 30, 40 };
    // inserts 3 at front
    auto it = vec.insert(vec.begin(), 3);
    // inserts 2 at front
    vec.insert(it, 2);
    int i = 2;
    // inserts 7 at i-th index
    it = vec.insert(vec.begin() + i, 7);
    cout << "The vector elements are: " ;
    for ( auto it = vec.begin(); it != vec.end(); ++it)
    cout << *it << " " ;
    return 0;
    }

    
    

    输出:

    The vector elements are: 2 3 7 10 20 30 40
    

    例2: 下面的程序演示了在特定位置插入新元素的上述功能。

    // Program below illustrates the
    // vector::insert() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    // initialising the vector
    vector< int > vec = { 10, 20, 70, 80 };
    int x = 50;
    // inserting multiple elements
    // at specific positions
    vec.insert(vec.begin() + 2, { 30, 40, x, 60 });
    cout << "The vector elements are: " ;
    for ( auto it : vec)
    cout << it << " " ;
    return 0;
    }

    
    

    输出:

    The vector elements are: 10 20 30 40 50 60 70 80
    

  2. 语法:
    vector_name.insert(position, size, val)
    

    参数: 该函数接受以下三个参数:

    • 位置—— 它指定指向插入位置的迭代器。
    • 尺寸- 它指定在指定位置插入val的次数。
    • 瓦尔—— 它指定要插入的值。

    返回值: 该函数返回一个迭代器,该迭代器指向新插入的元素。

    下面的程序说明了上述功能:

    // program below illustrates the
    // vector::insert() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    // initialising the vector
    vector< int > vec = { 10, 20, 30, 40 };
    // inserts 3 one time at front
    auto it = vec.insert(vec.begin(), 1, 3);
    // inserts 4 two times at front
    vec.insert(it, 2, 4);
    cout << "The vector elements are: " ;
    for ( auto it = vec.begin(); it != vec.end(); ++it)
    cout << *it << " " ;
    return 0;
    }

    
    

    输出:

    The vector elements are: 4 4 3 10 20 30 40
    

  3. 语法:
    vector_name.insert(position, iterator1, iterator2)
    

    参数: 该函数接受以下三个参数:

    • 位置—— 它指定在向量中插入的位置。
    • 迭代器1- 它指定要插入元素的起始位置
    • 迭代器2- 它指定插入元素之前的结束位置

    返回值: 该函数返回一个迭代器,该迭代器指向新插入的元素。

    以下是上述功能的说明:

    // program below illustrates the
    // vector::insert() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    // initialising the vector
    vector< int > vec1 = { 10, 20, 30, 40 };
    vector< int > vec2;
    // inserts at the beginning of vec2
    vec2.insert(vec2.begin(), vec1.begin(), vec1.end());
    cout << "The vector2 elements are: " ;
    for ( auto it = vec2.begin(); it != vec2.end(); ++it)
    cout << *it << " " ;
    return 0;
    }

    
    

    输出:

    The vector2 elements are: 10 20 30 40
    

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享