STL 容器概念(序列)
序列
////////////////////////////////////
deque list queue priority_queue stack vector
序列要求至少是正向迭代器 保证特定顺序排序 不会再两次迭代发生变化
X a(n,t) 初始化为n个t
X (n,t) 匿名序列初始化为n个t
X a (i,j) 初始化为[i,j)内容
X (i,j) 匿名序列初始化为[i,j)内容
a.insert(p,t) 返回值迭代器 将t插入到p前面
a.insert(p,n,t) 返回值void 将n个t插入到p前面
a.insert(p,i,j) 返回值void 将区间[i,j)内容插入到p前面
a.erase(p) 返回值迭代器 删除p指向的元素
a.erase(p,q) 返回值迭代器 删除区间[p,q]中元素
a.clear() 返回值void 等价a.erase(begin(),end())
要求严格线性排列
p622
*******************************************************************
1>vector
向量数组
数组的一种表示 自动内存管理 动态改变长度 元素随机访问 尾部插入删除元素时间固定
头部插入删除元素时间是线性 可反转容器
*******************************************************************
2>deque
双端队列
与vector类似 不表现同处是头部添加删除元素是时间固定 内部结构更复杂
*******************************************************************
3>list
双向链表
除头尾元素外 每个元素都与前后元素相连 可双向遍历链表
任何位子插入都是时间固定 元素插入删除速度快 可反转容器 并插入删除元素后迭代器指向不会变
不可以数组表示 不可以随机访问
// list.cpp -- using a list
#include <iostream>
#include <list>
#include <iterator>
int main()
{
using namespace std;
list<int> one(5, 2); //初始化5个2
int stuff[5] = {1,2,4,8, 6};
list<int> two;
two.insert(two.begin(),stuff, stuff + 5 );//把stuff值赋给two
int more[6] = {6, 4, 2, 4, 6, 5};
list<int> three(two); //初始化three为two
three.insert(three.end(), more, more + 6);
cout << "List one: ";
ostream_iterator<int,char> out(cout, " ");
copy(one.begin(), one.end(), out);
cout << endl << "List two: ";
copy(two.begin(), two.end(), out);
cout << endl << "List three: ";
copy(three.begin(), three.end(), out);
three.remove(2); //删除three里面所有为2的值
cout << endl << "List three minus 2s: ";
copy(three.begin(), three.end(), out);
three.splice(three.begin(), one); //把one转移入到three one变为空
cout << endl << "List three after splice: ";
copy(three.begin(), three.end(), out);
cout << endl << "List one: ";
copy(one.begin(), one.end(), out);
three.unique(); //联系相同元素压缩成单个
cout << endl << "List three after unique: ";
copy(three.begin(), three.end(), out);
three.sort();
three.unique();
cout << endl << "List three after sort & unique: ";
copy(three.begin(), three.end(), out);
two.sort();
three.merge(two); //将传入链表与调用链表合并(都要是排序过的)放入调用链表 传入链表变为空
cout << endl << "Sorted two merged into three: ";
copy(three.begin(), three.end(), out);
cout << endl;
return 0;
}
*******************************************************************
4>queue
适配器类
不允许随机访问 不允许遍历
队尾添加元素 队首删除元素 查看首尾值 检查元素数目 查看元素是否为空
bool empty()const 队列空返回true 不然返回false
size_type size()const 返回队列中元素数目
T & front()返回指向队首元素引用
T & back()返回指向队尾元素引用
void push(const T & x)在队尾插入X
void pop()删除队首元素
*******************************************************************
5>priority_queue
适配器类 (同queue声明文件一样)
与queue基本相同 只是最大的元素会被放到队首
两个构造函数 (可选确定那个元素被放队首的比较方式)
priority_queue<int> pq1;
priority_queue<int> pq2(greater<int>);
(greater<>()是个预定义函数对象)
*******************************************************************
6>stack
适配器类
不允许随机访问 不允许遍历堆栈
压入桟顶 桟顶弹出 查看桟顶值 检查元素数目 推测桟是否空
bool empty()const 堆栈空返回true 不然返回false
size_type() const返回堆栈中元素数目
T & top() 返回指向桟顶元素引用
void push(const T & x)在堆栈顶插入x
void pop() 删除桟顶元素


浙公网安备 33010602011771号