C++ STL中deque在开头插入和删除元素

C++ STL中deque在开头插入和删除元素

deque 是一个 STL 动态数组类,与 vector 非常类似,但支持在数组开头和末尾插入或删除元素。
要实例化一个整型 deque,可以像下面这样做:

// Define a deque of integers
std::deque <int> intDeque;  

要使用 std::deque,需要包含头文件:

#include<deque>  

deque 与 vector 极其相似,也支持使用函数 push_back( )和 pop_back( )在末尾插入和删除元素。与
vector 一样, deque 也使用运算符[]以数组语法访问其元素。 deque 与 vector 的不同之处在于,它还允许
您使用 push_front 和 pop_front 在开头插入和删除元素,如程序清单 17.8 所示。

0: #include <deque>
1: #include <iostream>
2: #include <algorithm>
3:
4: int main ()
5: {
6: using namespace std;
7:
8: // Define a deque of integers
9: deque<int> intDeque;
10:
11: // Insert integers at the bottom of the array
12: intDeque.push_back (3);
13: intDeque.push_back (4);
14: intDeque.push_back (5);
15:
16: // Insert integers at the top of the array
17: intDeque.push_front (2);
18: intDeque.push_front (1);
19: intDeque.push_front (0);
20:
21: cout << "The contents of the deque after inserting elements ";
22: cout << "at the top and bottom are:" << endl;
23:
24: // Display contents on the screen
25: for (size_t count = 0;
26: count < intDeque.size ();
27: ++ count )
28: {
29: cout << "Element [" << count << "] = ";
30: cout << intDeque [count] << endl;
31: }
32:
33: cout << endl;
34:
35: // Erase an element at the top
36: intDeque.pop_front ();
37:
38: // Erase an element at the bottom
39: intDeque.pop_back ();
40:
41: cout << "The contents of the deque after erasing an element ";
42: cout << "from the top and bottom are:" << endl;
43:
44: // Display contents again: this time using iterators
45: // if on older compilers, remove auto and uncomment next line
46: // deque <int>::iterator element;
47: for (auto element = intDeque.begin ();
48: element != intDeque.end ();
49: ++ element )
50: {
51: size_t Offset = distance (intDeque.begin (), element);
52: cout << "Element [" << Offset << "] = " << *element << endl;
53: }
54:
55: return 0;
56: }

输出:

The contents of the deque after inserting elements at the top and bottom are:
Element [0] = 0
Element [1] = 1
Element [2] = 2
Element [3] = 3
Element [4] = 4
Element [5] = 5
The contents of the deque after erasing an element from the top and bottom are:
Element [0] = 1
Element [1] = 2
Element [2] = 3
Element [3] = 4  

分析:
第 9 行实例化了一个整型 deque, 其语法与实例化整型 vector 极其相似。 第 12~14 行演示了 deque
的成员函数 push_back 的用法,然后第 17~19 行演示了 push_front()的用法, push_front()是 deque 唯一
不同于 vector 的地方。 pop_front()的用法类似,如第 36 行所示。要显示 deque 的内容,第一种方法是
使用数组语法访问其元素(如第 25~31 行所示),第二种方法是使用迭代器(如第 47~53 行所示)。
在第 51 行,使用了算法 std::distance()计算元素的偏移位置,这与程序清单 17.5 中处理 vector 时相同。

要清空 vector 和 deque 等 STL 容器,即删除其包含的所有元素,可使用函数 clear()。
下面的代码删除程序清单 17.7 中 vector integers 的所有元素:
integers.clear();
要删除程序清单 17.8 中 deque intDeque 的所有元素,可添加如下代码:
intDeque.clear();
请注意, vector 和 deque 还包含成员函数 empty(),这个函数在容器为空时返回 true,而
不像 clear()那样删除既有的元素。
intDeque.clear();
if (intDeque.empty())
cout << "The container is now empty" << endl;
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值