vector_pro::resize¶
void resize(const size_type re_size);
void resize(const size_type re_size, const value_type& val);
Change size¶
Resizes the container so that it contains re_size elements.
If re_size is smaller than the current container size, the content is reduced to its first re_size elements, removing those beyond (and destroying them).
Different from
std::vector, the capacity will shrink tore_size.
If re_size 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 re_size. If val is specified, the new elements are initialized as copies of val .
Different from
std::vector, ifvalis not specified,vector_prowill do nothing.
If re_size is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
Parameters¶
re_size
New container size, expressed in number of elements.val
Object whose content is copied to the added elements in case that n is greater than the current container size.
Return value¶
none
Example¶
// resizing vector_pro
#include <iostream>
#include "vector_pro.h"
/**
* Output:
* myvector contains: 1 2 3 4 5 100 100 100
*/
int main ()
{
vector_pro<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
Complexity¶
Linear on the number of elements inserted/erased (constructions/destructions).
If a reallocation happens, the reallocation is itself up to linear in the entire vector size.