Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
deque
comparison operators
deque::deque
deque::~deque
member functions:
deque::assign
deque::at
deque::back
deque::begin
deque::clear
deque::empty
deque::end
deque::erase
deque::front
deque::get_allocator
deque::insert
deque::max_size
deque::operator=
deque::operator[]
deque::pop_back
deque::pop_front
deque::push_back
deque::push_front
deque::rbegin
deque::rend
deque::resize
deque::size
deque::swap


deque::resize

public member functions
void resize ( size_type sz, T c = T() );

Change size

Resizes the container to contain sz elements.

If sz is smaller than the current container size, the content is reduced to its first sz elements, the rest being dropped.

If sz is greater than the current container size, the content is expanded by inserting at the end as many copies of c as needed to reach a size of sz elements.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.

Parameters

sz
New container size, expressed in number of elements.
Member type size_type is an unsigned integral type.
c
Object whose content is copied to the added elements in case that sz is greater than the current container size.
If not specified, the default constructor is used.
T is the first template parameter (the type of the elements stored in the container).

Return Value

none

In case of growth, the storage for the new elements is allocated using Allocator::allocate(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Example

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
27
// resizing deque
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
  deque<int> mydeque;
  deque<int>::iterator it;
  unsigned int i;
  // set some initial content:
  for (i=1;i<10;i++) mydeque.push_back(i);
  mydeque.resize(5);
  mydeque.resize(8,100);
  mydeque.resize(12);
  cout << "mydeque contains:";
  for (it=mydeque.begin(); it<mydeque.end(); ++it)
    cout << " " << *it;
  cout << endl;
  return 0;
}


The code sets a sequence of 9 numbers as an initial content for mydeque. It then uses resize first to set the container size to 5, then to extend its size to 8 with values of 100 for its new elements, and finally it extends its size to 12 with their default values (for int elements this is zero). Output:
mydeque contains: 1 2 3 4 5 100 100 100 0 0 0 0

Complexity

Linear on the number of elements inserted/erased (constructions/destructions).

See also