The <vector>
header is one of the standard container template headers. It declares the vector
class template and a few global functions that operate on vector
objects.
A vector is a sequence container that has linear performance for inserting and erasing at any point in the container, but supports random access iterators. A vector is best thought of as a generalization of arrays.
See Chapter 11 for information about containers in general.
Compare for equality
template <typename T, typename A> bool operator==(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator==(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Returns true if x
and y
have the same size and their elements are equal, that is, x.size()
==
y.size()
&&
equals(x.begin(),
x.end(),
y.begin())
.
equals in <algorithm>
Compare for inequality
template <typename T, typename A> bool operator!=(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator!=(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Returns !
(x
==
y)
.
Compare for less than
template <typename T, typename A> bool operator<(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator<(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Determines whether x
is less than y
, using the same algorithm as lexicographic_compare(x.begin(),
x.end(),
y.begin(),
y.end())
.
lexicographic_compare in <algorithm>
Compare for less than or equal
template <typename T, typename A> bool operator<=(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator<=(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Returns !
(y
<
x)
.
Compare for greater than
template <typename T, typename A> bool operator>(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator>(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Returns (y
<
x)
.
Compare for greater than or equal
template <typename T, typename A> bool operator>=(const vector<T,A>& x, const vector<T,A>& y); template <typename Alloc> bool operator>=(const vector<bool,Alloc>& x, const vector<bool,Alloc>& y);
Returns !
(x
<
y)
.
Swap contents of two vectors
template <typename T, typename Alloc> void swap(vector<T,Alloc>& x, vector<T,Alloc>& y); template <typename Alloc> void swap(vector<bool,Alloc>& x, vector<bool,Alloc>& y);
Calls x.swap(y)
.
swap in <algorithm>
Array-like container
template <typename T, typename Alloc = allocator<T> > class vector { public: typedef typename Alloc::reference reference; typedef typename Alloc::const_reference const_reference; typedef ... iterator; typedef ... const_iterator; typedef ... size_type; typedef ... difference_type; typedef T value_type; typedef Alloc allocator_type; typedef typename Alloc::pointer pointer; typedef typename Alloc::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; explicit vector(const Alloc& = Alloc()); explicit vector(size_type n, const T& value = T(), const Alloc& = Alloc()); template <class InpIt> vector(InpIt first, InpIt last, const Alloc& = Alloc()); vector(const vector<T,Alloc>& x); ~vector(); vector<T,Alloc>& operator=(const vector<T,Alloc>& x); template <class InputIterator> void assign(InputIterator first, InputIterator last); void assign(size_type n, const T& u); allocator_type get_allocator() const; iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const; reverse_iterator rbegin(); const_reverse_iterator rbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; size_type size() const; size_type max_size() const; void resize(size_type sz, T c = T()); size_type capacity() const; bool empty() const; void reserve(size_type n); // element access: reference operator[](size_type n); const_reference operator[](size_type n) const; const_reference at(size_type n) const; reference at(size_type n); reference front(); const_reference front() const; reference back(); const_reference back() const; // modifiers: void push_back(const T& x); void pop_back(); iterator insert(iterator position, const T& x); void insert(iterator position, size_type n, const T& x); template <class InpIt> void insert(iterator position, InpIt first, InpIt last); iterator erase(iterator position); iterator erase(iterator first, iterator last); void swap(vector<T,Alloc>&); void clear(); };
The vector
class template is a standard sequence container that is like an array: adding or removing from the end of the vector takes constant time (amortized over many such operations); adding or removing from anywhere else takes linear time; random access requires constant time.
Elements of a vector
are stored contiguously, just like an ordinary array. Most cases where you need an array, you should use a vector
instead because a vector
offers greater safety (no need for dynamic memory, at
member function, etc.)
All iterators become invalid when the array is resized. Iterators also become invalid when they are past (at a higher index) the point where an item is inserted or erased.
If you need a vector of characters, consider using a string (<string>
header) instead. A string meets all the requirements of a container, but has additional members for handling character and string information. If you need a vector of Boolean values, consider using deque<bool>
instead of vector<bool>
. See vec
tor
<bool>
for an explanation.
Following are the members of vector
:
explicit
vector
(
c
onst
Allo
c
&
= Alloc())
explicit
vector
(s
i
ze_type
n,
const
T&
value
=
T(),
const
Alloc&
= Alloc())
n
, where each element is initialized to value
.template <class InpIt>
vector
(
I
npIt
firs
t
,
InpIt
las
t
,
const
Alloc& =
Alloc())
first
, last
) into the new vector.
vector
(const vector<T,Alloc>& v)
v
.vecto
r
<T,Alloc>&
operator=
(const
vector<T,All
oc
>&
v)
v
into the vector.template <class InputIterator>
void
assign
(
I
nputIterator
firs
t
, InputIterator
la
st
)
first
, last
) into the vector.void
assign
(size_type
n,
const
T&
valu
e
)
n
copies of value
.const_
re
ference
at
(size_type n) const
reference
at
(size_type n)
n
. If n
>=
siz
e
()
, throws o
ut
_of_range
.
back
()
back
() constiterator
begin
()
const_
iterato
r
begin
()
const
size_
t
ype
capacity
()
const
void
clear
()
iterator
end
()
const_
i
terator
end
()
const
bool
empty
()
const
size()
==
0
.iterator
erase
(iterator position)
position
.iterator
erase
(
i
terator first, iterator last)
first
, last
).reference
front
()
const_
r
eference
front
()
const
locator_
t
ype
get_allocator
()
const
iterator
insert
(
ite
rator
positio
n
,
const
T&
x)
x
before position
.void
insert
(iterator
po
s
,
size_type
n,
const
T&
x)
n
copies of x
at pos
.template <class InpIt>
void
insert
(iterator
pos,
InpIt
first,
InpIt
last)
first
, last
) starting at position pos
.siz
e
_
t
ype
max_
s
ize
()
const
reference
operat
or
[]
(
s
ize_type
n)
const_reference
operator[]
(
s
ize_type
n)
const
n
. If n
>=
size()
, the behavior is undefined.void
pop_back
()
void
push_back
(const T& x)
x
as the new last element of the vector.reverse_iterator
rbegin
()
const_reverse_iterator
rbegin
()
const
reverse_
i
terator
rend
()
const_reverse_
i
terator
rend
()
const
void
reserve
(size_type n)
capacity()
is at least n
. Call reserve
to avoid the need to reallocate the vector repeatedly when you know the vector will grow by small increments to a large size. Note that size()
does not change.void
resize
(size_type
sz,
T c = T())
n
. If n
> size()
, one or more copies of c
are added to the end of the vector to reach the desired size. If the new size is smaller than the current size, elements are erased from the end to reach the new size.size_type
size
()
const
void
swap
(vector<T,Alloc>& that)
that
.vector<bool> class, deque in <deque>, list in <list>
Specialized vector of bool
template <typename Alloc> class vector<bool, Alloc> { public: typedef bool const_reference; typedef ... iterator; typedef ... const_iterator; typedef ... size_type; typedef ... difference_type; typedef bool value_type; typedef Alloc allocator_type; typedef ... pointer; typedef ... const_pointer typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; class reference; static void swap(reference x, reference y); void flip(); ... // same as vector<> ... };
The vector
<bool>
specialization is an interesting beast. It is an attempt to demonstrate how to define a container that uses a proxy to represent the elements of the container. The bool
elements are packed into integers, and the vector<bool>::reference
type is a proxy that represents a single bool
element by keeping track of the bit number within the integer and the integer's index in the vector.
By using a proxy, though, vecto
r
<bool>
violates the constraints of a container, so it cannot be used in many situations that call for a standard container. In particular, the pointer
type cannot point to an element of the container because C++ does not have a type that can point to a single bit. Many algorithms require the pointer type, and so cannot work with a vector<bool>
object.
If you need to use a compact set of bits, use the bitset
class template. If you need a standard container that contains bool elements, use deque<bool>
.
In addition to the members of the vector<>
template, vector<bool>
also defines the following functions:
static
void
swap
(reference x, reference y)
void
flip
()
vector class template, vector<bool>::reference class, bitset in <bitset>, deque in <deque>
Bit reference proxy
class reference { friend class vector; reference(); public: ~reference(); operator bool() const; reference& operator=(const bool x); reference& operator=(const reference& x); void flip(); };
The reference
class represents a single bit in a vector<bool>
. The constructor is private, so only vector<bool>
can create reference objects. The reference keeps track of the position of an individual bit in a vector<bool>
so you can get, set, or flip the bit. Following are the members of reference:
void
flip
()
*this
=
!
*this
.operator
bool
()
const
bool
.reference&
operator=
(const bool x)
reference&
operator=
(const reference& x)
x
to *this
.vector<bool> class