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
vector
comparison operators
vector::vector
vector::~vector
member functions:
vector::assign
vector::at
vector::back
vector::begin
vector::capacity
vector::clear
vector::empty
vector::end
vector::erase
vector::front
vector::get_allocator
vector::insert
vector::max_size
vector::operator=
vector::operator[]
vector::pop_back
vector::push_back
vector::rbegin
vector::rend
vector::reserve
vector::resize
vector::size
vector::swap


vector::vector

public member function
<vector>
explicit vector ( const Allocator& = Allocator() );
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
template <class InputIterator>
         vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
vector ( const vector<T,Allocator>& x );

Construct vector

Constructs a vector container object, initializing its contents depending on the constructor version used:

explicit vector ( const Allocator& = Allocator() );
Default constructor: constructs an empty vector, with no content and a size of zero.
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
Repetitive sequence constructor: Initializes the vector with its content set to a repetition, n times, of copies of value.
template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container.
vector ( const vector<T,Allocator>& x );
Copy constructor: The vector is initialized to have the same contents (copies) and properties as vector x.

Parameters

n
Times that value is repeated to form the content of the container object.
Member type size_type is an unsigned integral type.
value
Value to be repeated n times as the content of the container object.
T is the first class template parameter (the type of the elements stored in the vector).
first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template type can be any type of input iterator.
x
Another vector object with the same class template parameters (T and Allocator).
unnamed Allocator parameter
Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.

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
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
  unsigned int i;
  // constructors used in the same order as described above:
  vector<int> first;                                // empty vector of ints
  vector<int> second (4,100);                       // four ints with value 100
  vector<int> third (second.begin(),second.end());  // iterating through second
  vector<int> fourth (third);                       // a copy of third
  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {16,2,77,29};
  vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
  cout << "The contents of fifth are:";
  for (i=0; i < fifth.size(); i++)
    cout << " " << fifth[i];
  cout << endl;
  return 0;
}

Output:
The contents of fifth are: 16 2 77 29 

Complexity

For the default constructor, constant.
For the repetitive sequence constructor, linear on n (copy constructions).
For the iterator constructor, linear on the distance between the iterators (copy constructions). If the iterators are forward, bidirectional or random-access, the capacity is determined before beginning, otherwise logarithmic complexity (object reallocations) must be added on top of that.
For the copy constructor, linear on x's size (copy constructions).

See also