八、STL 顺序容器 vector、deque、list

来源:互联网 发布:五道口 知乎 编辑:程序博客网 时间:2024/05/22 17:41

STL 顺序容器 vector、deque、list

一、vector

1.1 vector 简介

vector 指动态数组。元素在内存连续存放。随机存取任何元素都能在常数时间完成,在尾端增删元素具有较佳的性能(大部分情况下是常数时间)。头文件是< vector> 。
vector

vector有以下几个特点:

  1. 支持随机访问迭代器,
  2. 根据下标随机访问某个元素时间为常数
  3. 在尾部添加速度很快
  4. 在中间插入慢
  5. 所有STL算法 都能对vector操作

1.2 成员函数

用于初始化vector的构造函数:
构造函数

其它成员函数:
其它成员函数

1.3 程序示例

#include <iostream>#include <vector>using namespace std;int main() {    int i;    int a[5] = {1,2,3,4,5};    vector<int> v(5);    cout << v.end() - v.begin() << endl;    for( i = 0; i < v.size(); i ++ ) v[i] = i;    v.at(4) = 100;    for( i = 0; i < v.size(); i ++ )        cout << v[i] << "," ;    cout << endl;    vector<int> v2(a,a+5); //构造函数    v2.insert(v2.begin() + 2,13); //在begin()+2位置插入13    for( i = 0; i < v2.size(); i ++ )        cout << v2.at(i) << "," ;}

输出:

5
0,1,2,3,100,
1,2,13,3,4,5,

二、list和deque

2.1 简介

deque指双向队列。元素在内存连续存放。随机存取任何元素都能在常数时间完成(但次于vector)。在两端增删元素具有较佳的性能(大部分情况下是常数时间)。 所有适用于vector的操作都适用于deque,deque还有 push_front (将元素插入到容器的头部)和 pop_front (删除头部的元素) 操作。
deque

list指双向链表。元素在内存不连续存放。在任何位置增删元素都能在常数时间完成。 不支持随机存取,具有所有顺序容器都有的成员函数。
list

2.2 list成员函数

成员函数

list容器的迭代器不支持完全随机访问,不能用标准库中sort函数对它进行排序。

但list有自己的sort成员函数,使用方法:

list<T> lstlst.sort(compare); //compare函数可以自己定义lst.sort(); //无参数版本, 按<排序

2.3 list程序示例

#include<iostream>#include<list>#include<algorithm>using namespace std;class A{    int v;    public:        A(int v):v(v) { }    friend bool operator<(const A & a1, const A & a2);    friend bool operator==(const A & a1, const A & a2);    friend ostream & operator<<(ostream & o, const A & a);};bool operator<(const A & a1, const A &a2){    return a1.v<a2.v;}bool operator==(const A & a1, const A &a2){    return a1.v==a2.v;}ostream & operator<<(ostream & o, const A & a){    cout<<a.v;    return o;}template <class T>void printList(const list<T> &lst){    if(lst.size() == 0) return;    typename list<T>::const_iterator i;    for(i=lst.begin(); i!=lst.end(); i++)        cout<<*i<<" ";    cout<<endl;}int main(){    list<A> lst1, lst2;    lst1.push_back(1); lst1.push_back(2); lst1.push_back(3);    lst1.push_back(4); lst1.push_back(5); lst1.push_back(6);    lst2.push_back(10);    lst2.push_front(20);    lst2.push_back(20);    lst2.push_front(30);    lst2.push_back(30);    lst2.push_back(30);    lst2.push_back(40);    printList(lst1);    printList(lst2);    lst2.sort();    printList(lst2);    lst2.pop_front();    printList(lst2);    lst1.remove(2);    printList(lst1);    lst2.unique();    printList(lst2);    lst1.merge(lst2);    printList(lst1);    printList(lst2);    lst1.reverse();    printList(lst1);    lst2.push_back(100);    lst2.push_back(200);    lst2.push_back(300);    lst2.push_back(400);    list<A>::iterator p1, p2, p3;    p1 = find(lst1.begin(), lst1.end(), 3);    p2 = find(lst2.begin(), lst2.end(), 200);    p3 = find(lst2.begin(), lst2.end(), 400);    lst1.splice(p1, lst2, p2, p3);    printList(lst1);    printList(lst2);    return 0;}

输出:

1 2 3 4 5 6
30 20 10 20 30 30 40
10 20 20 30 30 30 40
20 20 30 30 30 40
1 3 4 5 6
20 30 40
1 3 4 5 6 20 30 40
40 30 20 6 5 4 3 1
40 30 20 6 5 4 200 300 3 1
100 400

0 0