C++ STL vector使用insert在指定位置插入元素

C++ STL vector使用insert在指定位置插入元素

push_back()在 vector 末尾插入元素。如果要在中间插入元素,该如何办呢?很多 STL 容器(包括
std::vector)都包含 insert( )函数,且有多个重载版本。
其中一个版本让您能够指定插入位置:
// insert an element at the beginning
integers.insert (integers.begin (), 25);
另一个版本让您能够指定插入位置、要插入的元素数以及这些元素的值(都相同):
// Insert 2 elements of value 45 at the end
integers.insert (integers.end (), 2, 45);
还可将另一个 vector 的内容插入到指定位置:
// Another vector containing 2 elements of value 30
vector another (2, 30);
// Insert two elements from another container in position [1]
integers.insert (integers.begin () + 1,
another.begin (), another.end ());
可使用迭代器(通常是由 begin( )或 end( )返回的)告诉 insert( )您想将新元素插入到什么位置。
也可将该迭代器设置为 STL 算法(如 std::find( )函数)的返回值。 std::find( )可用于查找
元素,然后在这个位置插入另一个元素(这将导致找到的元素向后移)。诸如 find()等算
法将在第 23 章详细讨论。
程序清单 17.3 演示了 vector::insert( )的各种重载版本。

0: #include <vector>
1: #include <iostream>
2: using namespace std;
3:
4: void DisplayVector(const vector<int>& inVec)
5: {
6: for (auto element = inVec.cbegin();
7: element != inVec.cend();
8: ++ element )
9: cout << *element << ' ';
10:
11: cout << endl;
12: }
13:
14: int main ()
15: {
16: // Instantiate a vector with 4 elements, each initialized to 90
17: vector <int> integers (4, 90);
18:
19: cout << "The initial contents of the vector: ";
20: DisplayVector(integers);
21:
22: // Insert 25 at the beginning
23: integers.insert (integers.begin (), 25);
24:
25: // Insert 2 numbers of value 45 at the end
26: integers.insert (integers.end (), 2, 45);
27:
28: cout << "Vector after inserting elements at beginning and end: ";
29: DisplayVector(integers);
30:
31: // Another vector containing 2 elements of value 30
32: vector <int> another (2, 30);
33:
34: // Insert two elements from another container in position [1]
35: integers.insert (integers.begin () + 1,
36: another.begin (), another.end ());
37:
38: cout << "Vector after inserting contents from another vector: ";
39: cout << "in the middle:" << endl;
40: DisplayVector(integers);
41:
42: return 0;
43: }

输出:
The initial contents of the vector: 90 90 90 90
Vector after inserting elements at beginning and end: 25 90 90 90 90 45 45
Vector after inserting contents from another vector: in the middle:
25 30 30 90 90 90 90 45 45
分析:
上述代码演示了 insert()函数的强大功能,它让您能够将值插入到容器中间。第 17 行的 vector 包含
4 个元素,每个元素都被初始化为 90;然后,使用了成员函数 vector::insert()的各种重载版本。第 23
行在开头添加了一个元素;第 26 行在末尾添加了两个元素,它们的值都是 45。第 35 行演示了如何将
一个 vector 的元素插入到另一个 vector 中间(这里是第一个元素后面,偏移量为 1)。

虽然函数 vector::insert()用途广泛,但给 vector 添加元素时,应首选 push_back( )。这是因为将元素
插入 vector 时, insert( )可能是效率最低的(插入位置不是末尾时),因为在开头或中间插入元素时,将
导致 vector 类将后面的所有元素后移(为要插入的元素腾出空间)。根据容器中包含的对象类型,这种
移动操作可能需要调用复制构造函数或赋值运算符,因此开销可能很大。在上述例子中, vector 包含
的是 int 对象,移动开销不是很大。但在其他情况下,情况可能并非如此。
如果需要频繁地在容器中间插入元素,应选择使用第 18 章将介绍的 std::list。

快来学习C++吧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值