码迷,mamicode.com
首页 > 其他好文 > 详细

STL之deque

时间:2015-06-04 22:37:29      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

参见http://www.cplusplus.com/reference/deque/deque/

deque是双向开口的连续性存储空间。虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储区本身是连续的,deque自身的机制把这一块一块的存储区虚拟地连在一起。

template < class T, class Alloc = allocator<T> > class deque;

Double ended queue
deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).
[双端队列deque是double-ended queue的不规则缩写形式。双端队列是顺序容器,其长度可以动态变化]
Specific libraries may implement deques in different ways, generally as some form of dynamic array. But in any case, they allow for the individual elements to be accessed directly through random access iterators, with storage handled automatically by expanding and contracting the container as needed.
[不同的库对于双端队列的实现可能会有不同方式,一般是以某种形式的动态数组来实现,但无论何种形式都得允许通过随机存储迭代器来直接获取单个元素]
Therefore, they provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations
[因此,双端队列提供了一个类似与vector的功能,但是又考虑到要在两端进行高效的插入与删除操作,因此,不同于vector,双端队列不保证将所有元素存储在连续的内存空间]
Both vectors and deques provide a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors use a single array that needs to be occasionally reallocated for growth, the elements of a deque can be scattered in different chunks of storage, with the container keeping the necessary information internally to provide direct access to any of its elements in constant time and with a uniform sequential interface (through iterators). Therefore, deques are a little more complex internally than vectors, but this allows them to grow more efficiently under certain circumstances, especially with very long sequences, where reallocations become more expensive.
vector和deque提供了相似的接口和功能,但两者内部的工作方式截然不同。vector使用的是一个单一的数组,并且为了增加容量,vector偶尔会进行再分配;而deque中的元素则会分散到不同的内存块,这也要求deque内部需要保存必要的信息,以此来以常数时间的时间复杂度直接获取任意元素。因此,deque的内部实现比vector稍复杂,但也因此使得deque可以在某些环境下可以高效地增长,尤其是对于一个长序列来说,此时再分配的代价会十分之大]
For operations that involve frequent insertion or removals of elements at positions other than the beginning or the end, deques perform worse and have less consistent iterators and references than lists and forward lists.
[如果要在除了头部和尾部之外的地方进行频繁的插入与删除操作,deque的表现会比list糟糕]

/*
//construct deque
deque (const allocator_type& alloc = allocator_type());
deque (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
deque (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
deque (const deque& x);
*/

#include <iostream>
#include <deque>

int main()
{
    // constructors used in the same order as described above:
    std::deque<int> first;                                // empty deque of ints
    std::deque<int> second (4,100);                       // four ints with value 100
    std::deque<int> third (second.begin(),second.end());  // iterating through second
    std::deque<int> fourth (third);                       // a copy of third

    // the iterator constructor can be used to copy arrays:
    int myints[] = {16,2,77,29};
    std::deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

    std::cout << "The contents of fifth are:";
    for (std::deque<int>::iterator it = fifth.begin(); it!=fifth.end(); ++it)
        std::cout <<   << *it;

    std::cout << \n;

    system("pause");
    return 0;
}
/*
size_type size() const;
bool empty() const;
void clear();
void swap();

reference back();
reference front();
reference at(size_type n);
reference operator[](size_type n);
deque& operator(const deque& x);

void push_back(const value_type& val);
void pop_back();
void push_front(const value_type& val);
void pop_front();

iterator begin();
iterator end();
reverse_iterator rbegin();
reverse_iterator rend();
*/

#include <iostream>
#include <deque>

int main ()
{
    std::deque<unsigned> mydeque (10);   // 10 zero-initialized unsigneds

    // assign some values:
    for (unsigned i=0; i<mydeque.size(); i++)
        mydeque.at(i)=i;

    std::cout << "mydeque contains:";
    for (unsigned i=0; i<mydeque.size(); i++)
        std::cout <<   << mydeque.at(i);

    std::cout << \n;

    system("pause");
    return 0;
}
/*
iterator insert (iterator position, const value_type& val);
void insert (iterator position, size_type n, const value_type& val);
void insert (iterator position, InputIterator first, InputIterator last);

Insert elements
The deque container is extended by inserting new elements before the element at the specified position.
[在position所指向的元素之前插入新元素]
This effectively increases the container size by the amount of elements inserted.
[该操作能够高效的增加容器的长度]
Double-ended queues are designed to be efficient performing insertions (and removals) from either the end or the beginning of the sequence. Insertions on other positions are usually less efficient than in list or forward_list containers.
[双端队列是为了在队列两端进行高效的插入和删除而设计的,在其他地方进行插入操作通常会比list低效]

Return value
An iterator that points to the first of the newly inserted elements.
[返回值是指向第一个新插入元素的迭代器(如果函数有返回值的话)]
Member type iterator is a random access iterator type that points to elements.
[iterator是一个指向元素的随机存储迭代器]
*/

#include <iostream>
#include <deque>
#include <vector>

int main ()
{
    std::deque<int> mydeque;

    // set some initial values:
    for (int i=1; i<6; i++) mydeque.push_back(i); // 1 2 3 4 5

    std::deque<int>::iterator it = mydeque.begin();
    ++it;

    it = mydeque.insert (it,10);                  // 1 10 2 3 4 5
    // "it" now points to the newly inserted 10

    mydeque.insert (it,2,20);                     // 1 20 20 10 2 3 4 5
    // "it" no longer valid!

    it = mydeque.begin()+2;

    std::vector<int> myvector (2,30);
    mydeque.insert (it,myvector.begin(),myvector.end());
    // 1 20 30 30 20 10 2 3 4 5

    std::cout << "mydeque contains:";
    for (it=mydeque.begin(); it!=mydeque.end(); ++it)
        std::cout <<   << *it;
    std::cout << \n;

    system("pause");
    return 0;
}
/*
iterator erase (iterator position);
iterator erase (iterator first, iterator last);

Erase elements
Removes from the deque container either a single element (position) or a range of elements ([first,last)).
[从容器中删除一个元素(position处)或者一组元素(范围是[first, last])]
This effectively reduces the container size by the number of elements removed, which are destroyed.
[该操作能够高效地缩小vector的长度]
Double-ended queues are designed to be efficient removing (and inserting) elements at either the end or the beginning of the sequence. Removals on other positions are usually less efficient than in list or forward_list containers.
[双端队列是为了在队列两端进行高效的插入和删除而设计的,在其他地方进行插入操作通常会比list低效]

Return value
An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.
[返回值是最后一个被删除元素的的下一个元素的迭代器,如果删除操作发生在尾部,则返回的是end]
*/

#include <iostream>
#include <deque>

int main ()
{
    std::deque<int> mydeque;

    // set some values (from 1 to 10)
    for (int i=1; i<=10; i++) mydeque.push_back(i);

    // erase the 6th element
    mydeque.erase (mydeque.begin()+5);

    // erase the first 3 elements:
    mydeque.erase (mydeque.begin(),mydeque.begin()+3);

    std::cout << "mydeque contains:";
    for (std::deque<int>::iterator it = mydeque.begin(); it!=mydeque.end(); ++it)
        std::cout <<   << *it;
    std::cout << \n;

    system("pause");
    return 0;
}
/*
void assign (InputIterator first, InputIterator last);
void assign (size_type n, const value_type& val);

Assign container content
Assigns new contents to the deque container, replacing its current contents, and modifying its size accordingly.
[重新分配deque的元素并相应改变deque的长度]
Any elements held in the container before the call are destroyed and replaced by newly constructed elements (no assignments of elements take place).
[容器中原有元素均被销毁并替换为新元素]
*/

#include <iostream>
#include <deque>

int main ()
{
    std::deque<int> first;
    std::deque<int> second;
    std::deque<int> third;

    first.assign (7,100);             // 7 ints with a value of 100

    std::deque<int>::iterator it;
    it=first.begin()+1;

    second.assign (it,first.end()-1); // the 5 central values of first

    int myints[] = {1776,7,4};
    third.assign (myints,myints+3);   // assigning from array.

    std::cout << "Size of first: " << int (first.size()) << \n;
    std::cout << "Size of second: " << int (second.size()) << \n;
    std::cout << "Size of third: " << int (third.size()) << \n;

    system("pause");
    return 0;
}
/*
void resize (size_type n, value_type val = value_type());
Change size
Resizes the container so that it contains n elements.
[改变容器的大小使之长度为n]
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
[如果n小于当前容器长度,则只保留前n个元素,其余元素销毁]
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
[如果n大于当前容器的长度,则用指定元素填充剩下的元素;若没有指定填充元素,则用默认值填充]
*/

#include <iostream>
#include <deque>

int main ()
{
    std::deque<int> mydeque;
    std::deque<int>::iterator it;

    // set some initial content:
    for (int i=1; i<10; ++i) mydeque.push_back(i);

    mydeque.resize(5);
    mydeque.resize(8,100);
    mydeque.resize(12);

    std::cout << "mydeque contains:";
    for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it)
        std::cout <<   << *it;
    std::cout << \n;

    system("pause");
    return 0;
}

STL之deque

标签:

原文地址:http://www.cnblogs.com/kevinq/p/4553084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!