|
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
|