STL为用户提供了多种名为容器(Container)的类,用于管理数据集合。
在创建动态数组、表、栈、队列、地图等数据结构时,我们只需定义对应的容器,包含其头文件,然后调用相应成员函数或算法即可。
C++ STL 最为基本的容器(Container)如下所示:
stack:栈
queue:队列
vector:向量
list:双向链表
set:集合
map:地图
string:字符串
deque:双端队列
priority_queue:优先队列
对于容器,主要的操作有:
容器的建立、插入元素、删除元素、查询、遍历、计算元素个数、检查元素是否为空、、输出容器包含的内容等
下面将依次介绍这些容器,并给出一些最常见的最实用的使用方法,做到快速入门。
一.Stack(栈)
栈是一种“先进后出”的数据结构。
栈中的各项操作复杂度均为O(1)
图1 Stack图解
表1 stack的成员函数示例
函数名 | 功能 | 复杂度 |
size() | 返回栈中的元素数 | O(1) |
top() | 返回栈顶的元素 | O(1) |
pop() | 从栈中取出并删除元素 | O(1) |
push(x) | 向栈中添加元素x | O(1) |
empty() | 在栈为空时返回true | O(1) |
stack的使用示例:
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.
The functions associated with stack are:
empty() – Returns whether the stack is empty
size() – Returns the size of the stack
top() – Returns a reference to the top most element of the stack
push(g) – Adds the element ‘g’ at the top of the stack
pop() – Deletes the top most element of the stack
#include <iostream>
#include <stack>
using namespace std;
template<class T>
void showstack(stack<T> s)
{
while (!s.empty()) // 如果栈s非空
{
cout << ' ' << s.top(); //输出栈顶元素
s.pop(); // 将栈顶元素弹出栈
}
cout << '\n';
}
int main ()
{
stack <int> s;
s.push(10); // 将元素10压入栈中
s.push(30);
s.push(20);
s.push(5);
s.push(1);
cout << "The stack s is : ";
showstack<int>(s); // 显示栈中的元素
cout << "\ns.size() : " << s.size(); // 输出栈的大小(即:栈中元素的个数)
cout << "\ns.top() : " << s.top(); // 输出暂定元素
cout << "\ns.pop() : ";
s.pop(); // 将栈顶元素弹出栈
showstack<int>(s); // 显示栈中的元素(此时栈中元素已全部出栈)
return 0;
}
二.queue(队列)
队列是一种“先进先出”的数据结构,类似于排队,先排者先享受服务。
队列的各项操作的时间复杂度均为O(1)
图2 队列图解
表2 queue的成员函数示例
函数名 | 功能 | 复杂度 |
size() | 返回队列中的元素数 | O(1) |
front() | 返回队头的元素 | O(1) |
back() | 返回队尾元素 | O(1) |
pop() | 从队列中取出并删除元素 | O(1) |
push(x) | 向队列中添加元素x | O(1) |
empty() | 在队列为空时返回true | O(1) |
queue的使用示例:
/*The functions supported by queue are :
1.empty() – Returns whether the queue is empty
2.size() – Returns the size of the queue
3.front() – Returns a reference to the first element of the queue
4.back() – Returns a reference to the last element of the queue
5.push(g) – Adds the element ‘g’ at the end of the queue
6.pop() – Deletes the first element of the queue*/
// CPP code to illustrate
// Queue in Standard Template Library (STL)
#include <iostream>
#include <queue>
using namespace std;
void showq(queue <int> gq)
{
queue <int> g = gq;
while (!g.empty())
{
cout << '\t' << g.front();
g.pop();
}
cout << '\n';
}
int main()
{
queue <int> gquiz;
gquiz.push(10);
gquiz.push(20);
gquiz.push(30);
cout << "The queue gquiz is : ";
showq(gquiz);
cout << "\ngquiz.size() : " << gquiz.size();
cout << "\ngquiz.front() : " << gquiz.front();
cout << "\ngquiz.back() : " << gquiz.back();
cout << "\ngquiz.pop() : ";
gquiz.pop();
showq(gquiz);
return 0;
}
三.vector(向量)
向量与动态数组相同,能够在插入或删除元素时自动调整自身大小,其存储由容器自动处理。 向量元素放置在连续存储中,以便可以使用迭代器访问和遍历它们。 在向量中,最后插入数据。 最后插入需要不同的时间,因为有时可能需要扩展阵列。 删除最后一个元素只需要一个恒定的时间,因为没有调整大小。 在开头或中间插入和删除是线性的。