deque操作和vector操作基本相同

构造函数

  1. default (1)

     explicit deque (const allocator_type& alloc = allocator_type());
    
  2. fill (2)

     explicit deque (size_type n);
          deque (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());
    
  3. range (3)

     template <class InputIterator>
       deque (InputIterator first, InputIterator last,
              const allocator_type& alloc = allocator_type());
    
  4. copy (4)

     deque (const deque& x);
     deque (const deque& x, const allocator_type& alloc);
    
  5. move (5)

     deque (deque&& x);
     deque (deque&& x, const allocator_type& alloc);
     initializer list (6)
     deque (initializer_list<value_type> il,
    		const allocator_type& alloc = allocator_type());
    

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// constructing deques
#include <iostream>
#include <deque>

int main ()
{
  unsigned int i;

  // 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';

  return 0;
}

赋值运算符

  1. copy (1)

     deque& operator= (const deque& x);
    
  2. move (2)

     deque& operator= (deque&& x);
    
  3. initializer list (3)

     deque& operator= (initializer_list<value_type> il);
    

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// assignment operator with deques
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> first (3);    // deque with 3 zero-initialized ints
  std::deque<int> second (5);   // deque with 5 zero-initialized ints

  second = first;
  first = std::deque<int>();

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  return 0;
}

迭代器

访问元素

容量

shrink_to_fit函数

void shrink_to_fit();

缩小以适合

请求容器减少其内存使用量以适应其大小。

一个deque容器可能具有比需要的更多内存来保存其当前元素:这是因为大多数库将deque作为一个动态数组来实现,该动态数组可以保留被删除元素的分配空间,或提前分配额外的容量以允许更快的插入操作。

该函数要求内存使用量适应容器的当前大小,但请求是非约束的,否则容器实现可以自由优化其内存使用。

请注意,此功能不会更改容器的大小

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> mydeque (100);
  std::cout << "1. size of mydeque: " << mydeque.size() << '\n';

  mydeque.resize(10);
  std::cout << "2. size of mydeque: " << mydeque.size() << '\n';

  mydeque.shrink_to_fit();

  return 0;
}

修改

##erase函数

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

指向由函数调用erase的最后一个元素后面的元素的新位置的迭代器。

如果操作擦除了序列中的最后一个元素,则这是容器结束。

分配器

重载

##relational operators (1)

template <class T, class Alloc>
  bool operator== (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

(2)

template <class T, class Alloc>
  bool operator!= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

(3)

template <class T, class Alloc>
  bool operator<  (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

(4)

template <class T, class Alloc>
  bool operator<= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

(5)

template <class T, class Alloc>
  bool operator>  (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

(6)

template <class T, class Alloc>
  bool operator>= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);

举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> foo (3,100);   // three ints with a value of 100
  std::deque<int> bar (2,200);   // two ints with a value of 200

  if (foo==bar) std::cout << "foo and bar are equal\n";
  if (foo!=bar) std::cout << "foo and bar are not equal\n";
  if (foo< bar) std::cout << "foo is less than bar\n";
  if (foo> bar) std::cout << "foo is greater than bar\n";
  if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
  if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";

  return 0;
}