C++ STL中在list中间插入元素

C++ STL中在list中间插入元素

std::list 的特点之一是,在其中间插入元素所需的时间是固定的,这项工作是由成员函数 insert()完
成的。
成员函数 list::insert()有 3 种版本。
• 第 1 种版本:
iterator insert(iterator pos, const T& x)
在这里, insert 函数接受的第 1 个参数是插入位置,第 2 个参数是要插入的值。该函数返回一
个迭代器,它指向刚插入到 list 中的元素。
• 第 2 种版本:
void insert(iterator pos, size_type n, const T& x)
该函数的第 1 个参数是插入位置,最后一个参数是要插入的值,而第 2 个参数是要插入的元素
个数。
• 第 3 种版本:
template
void insert(iterator pos, InputIterator f, InputIterator l)
该重载版本是一个模板函数,除一个位置参数外,它还接受两个输入迭代器,指定要将集合中
相应范围内的元素插入到 list 中。注意,输入类型 InputIterator 是一种模板参数化类型,因此
可指定任何集合(数组、 vector 或另一个 list)的边界。
程序清单 18.3 演示了如何使用函数 list::insert()的这些重载版本。

0: #include <list>
1: #include <iostream>
2: using namespace std;
3:
4: template <typename T>
5: void DisplayContents (const T& container)
6: {
7: for (auto element = container.cbegin();
8: element != container.cend();
9: ++ element )
10: cout << *element << ' ';
11:
12: cout << endl;
13: }
14:
15: int main ()
16: {
17: list <int> linkInts1;
18:
19: // Inserting elements at the beginning...
20: linkInts1.insert (linkInts1.begin (), 2);
21: linkInts1.insert (linkInts1.begin (), 1);
22:
23: // Inserting an element at the end...
24: linkInts1.insert (linkInts1.end (), 3);
25:
26: cout << "The contents of list 1 after inserting elements:" << endl;
27: DisplayContents (linkInts1);
28:
29: list <int> linkInts2;
30:
31: // Inserting 4 elements of the same value 0...
32: linkInts2.insert (linkInts2.begin (), 4, 0);
33:
34: cout << "The contents of list 2 after inserting '";
35: cout << linkInts2.size () << "' elements of a value:" << endl;
36: DisplayContents (linkInts2);
37:
38: list <int> linkInts3;
39:
40: // Inserting elements from another list at the beginning...
41: linkInts3.insert (linkInts3.begin (),
42: linkInts1.begin (), linkInts1.end ());
43:
44: cout << "The contents of list 3 after inserting the contents of ";
45: cout << "list 1 at the beginning:" << endl;
46: DisplayContents (linkInts3);
47:
48: // Inserting elements from another list at the end...
49: linkInts3.insert (linkInts3.end (),
50: linkInts2.begin (), linkInts2.end ());
51:
52: cout << "The contents of list 3 after inserting ";
53: cout << "the contents of list 2 at the end:" << endl;
54: DisplayContents (linkInts3);
55:
56: return 0;
57: }

输出:

The contents of list 1 after inserting elements:
1 2 3
The contents of list 2 after inserting '4' elements of a value:
0 0 0 0
The contents of list 3 after inserting the contents of list 1 at the beginning:
1 2 3
The contents of list 3 after inserting the contents of list 2 at the end:
1 2 3 0 0 0 0

分析:
在程序清单 18.3 中, begin()和 end()是 list 的成员函数,分别返回指向 list 开头和末尾的迭代器;
几乎都所有 STL 容器来说都如此。函数 list::insert()接受一个迭代器参数,元素将插入到该参数指定的
位置前面。 end()函数返回的迭代器(如第 24 行所示)指向 list 中最后一个元素的后面,因此这行代码
将 3 插入到末尾。第 32 行在一个 list 开头插入了 4 个元素,这些元素的值都为 0。第 41 行和第 42 行
演示了如何将一个 list 的内容插入到另一个 list 开头。虽然这里演示的是将一个整型 list 插入到另一个
list 中,但也可将插入范围指定为 vector 的边界(像程序清单 18.1 那样使用 begin( )和 end( ))或普通静
态数组的边界。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值