您的位置:首页 > 产品设计 > UI/UE

3 sequence containers in STL

2015-09-19 04:16 471 查看
Vector:

vector is a dynamic array capable of growing as needed to contain its elements.

Support operator[],insert and removing elements from the end of the vector is generally fast.

vector.front(),vector.back();

vector.push_back(), vector.pop_back();

vector.capacity() is different from vector.size();

capacity usually allocate twice time of current size.

vector.reserve(); reserve the size of vector.

Deque (pronounced “deck”)

a double-ended queue class,implemented as a dynamic array that can grow from both ends.

#include <iostream>
#include <deque>
int main()
{
using namespace std;

deque<int> deq;
for (int nCount=0; nCount < 3; nCount++)
{
deq.push_back(nCount); // insert at end of array
deq.push_front(10 - nCount); // insert at front of array
}

for (int nIndex=0; nIndex < deq.size(); nIndex++)
cout << deq[nIndex] << " ";

cout << endl;
}


This program produces the result:

8 9 10 0 1 2

List

a doubly linked list where each element in the container contains pointers that point at the next and previous elements in the list.

**Lists only provide access to the start and end of the list**there is no random access provided

The advantage of lists is that inserting elements into a list is very fast if you already know where you want to insert them. Generally iterators are used to walk through the list.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: