<vector>


Include the STL standard header <vector> to define the container template class vector and several supporting templates.

namespace std {
template<class Ty, class Alloc>
    class vector;
template<class Alloc>
    class vector<bool>;

template<class Alloc>
    struct hash<vector<bool, Alloc> >;  [added with C++11]

        // TEMPLATE FUNCTIONS
template<class Ty, class Alloc>
    bool operator==(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);
template<class Ty, class Alloc>
    bool operator!=(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);
template<class Ty, class Alloc>
    bool operator<(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);
template<class Ty, class Alloc>
    bool operator>(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);
template<class Ty, class Alloc>
    bool operator<=(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);
template<class Ty, class Alloc>
    bool operator>=(
        const vector<Ty, Alloc>& left,
        const vector<Ty, Alloc>& right);

template<class Ty, class Alloc>
    void swap(
        vector<Ty, Alloc>& left,
        vector<Ty, Alloc>& right);
}  // namespace std

hash

template<class Alloc>
    struct hash<vector<bool, Alloc> > [added with C++11]
        : public unary_function<vector<bool, Alloc>, size_t> {
    size_t operator()(vector<bool, Alloc> val) const;
    };

The template class defines its member function as returning a value uniquely determined by val. The member function defines a hash function, suitable for mapping values of type vector<bool, Alloc> to a distribution of index values.

operator!=

template<class Ty, class Alloc>
    bool operator!=(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function returns !(left == right).

operator==

template<class Ty, class Alloc>
    bool operator==(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function overloads operator== to compare two objects of template class vector. The function returns left.size() == right.size() && equal(left. begin(), left. end(), right.begin()).

operator<

template<class Ty, class Alloc>
    bool operator<(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function overloads operator< to compare two objects of template class vector. The function returns lexicographical_compare(left. begin(), left. end(), right.begin(), right.end()).

operator<=

template<class Ty, class Alloc>
    bool operator<=(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function returns !(right < left).

operator>

template<class Ty, class Alloc>
    bool operator>(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function returns right < left.

operator>=

template<class Ty, class Alloc>
    bool operator>=(
        const vector <Ty, Alloc>& left,
        const vector <Ty, Alloc>& right);

The template function returns !(left < right).

swap

template<class Ty, class Alloc>
    void swap(
        vector <Ty, Alloc>& left,
        vector <Ty, Alloc>& right);

The template function executes left.swap(right).

vector


allocator_type · assign · at · back · begin · capacity · cbegin · cend · clear · const_iterator · const_pointer · const_reference · const_reverse_iterator · crbegin · crend · data · difference_type · emplace · emplace_back · empty · end · erase · front · get_allocator · insert · iterator · max_size · operator= · operator[] · pointer · pop_back · push_back · rbegin · reference · rend · reserve · resize · reverse_iterator · shrink_to_fit · size · size_type · swap · value_type · vector


template<class Ty, class Alloc = allocator<Ty> >
    class vector {
public:
    typedef Alloc allocator_type;
    typedef typename Alloc::pointer pointer;
    typedef typename Alloc::const_pointer
        const_pointer;
    typedef typename Alloc::reference reference;
    typedef typename Alloc::const_reference
        const_reference;
    typedef typename Alloc::value_type value_type;
    typedef typename Alloc::size_type size_type;
    typedef typename Alloc::difference_type difference_type;

    typedef T0 iterator;
    typedef T1 const_iterator;
    typedef reverse_iterator<const_iterator>
        const_reverse_iterator;
    typedef reverse_iterator<iterator>
        reverse_iterator;

    vector();
    explicit vector(const Alloc& al);

    explicit vector(size_type count);
    vector(size_type count, const Ty& val);
    vector(size_type count, const Ty& val,
        const Alloc& al);

    vector(const vector& right);
    vector(const vector& right,
        const Alloc& al); [added with C++11]
    vector(vector&& right); [added with C++11]
    vector(vector&& right,
        const Alloc& al); [added with C++11]

    template<class InIt>
        vector(InIt first, InIt last);
    template<class InIt>
        vector(InIt first, InIt last, const Alloc& al);

    vector(initializer_list<Ty> init) [added with C++11]
    vector(initializer_list<Ty> init,
        const Alloc& al); [added with C++11]

    vector& operator=(const vector& right);
    vector& operator=(initializer_list<Ty> init) [added with C++11]
    vector& operator=(vector&& right); [added with C++11]

    iterator begin() noexcept;
    const_iterator begin() const noexcept;
    iterator end() noexcept;
    const_iterator end() const noexcept;
    reverse_iterator rbegin() noexcept;
    const_reverse_iterator rbegin() const noexcept;
    reverse_iterator rend() noexcept;
    const_reverse_iterator rend() const noexcept;

    const_iterator cbegin() const noexcept; [added with C++11]
    const_iterator cend() const noexcept; [added with C++11]
    const_reverse_iterator crbegin() const noexcept; [added with C++11]
    const_reverse_iterator crend() const noexcept; [added with C++11]

    Ty *data() noexcept; [added with C++11]
    const Ty *data() const noexcept; [added with C++11]

    void reserve(size_type count);
    size_type capacity() const noexcept;

    void shrink_to_fit(); [added with C++11]
    void resize(size_type newsize);
    void resize(size_type newsize, const Ty& val);
    size_type size() const noexcept;
    size_type max_size() const noexcept;
    bool empty() const noexcept;

    Alloc get_allocator() const noexcept;

    reference at(size_type off);
    const_reference at(size_type off) const;
    reference operator[](size_type off);
    const_reference operator[](size_type off) const;
    reference front();
    const_reference front() const;
    reference back();
    const_reference back() const;

    void push_back(const Ty& val);
    void push_back(Ty&& val); [added with C++11]
    template<class... Valty>
        void emplace_back(Ty&&... val); [added with C++11]
    void pop_back();

    template<class InIt>
        void assign(InIt first, InIt last);
    void assign(size_type count, const Ty& val);
    void assign(initializer_list<Ty> init) [added with C++11]

    iterator insert(const_iterator where, const Ty& val);
    iterator insert(const_iterator where, size_type count, const Ty& val);
    template<class InIt>
        iterator insert(const_iterator where, InIt first, InIt last);
    iterator insert(const iterator where,
        initializer_list<Ty> init) [added with C++11]
    iterator insert(const_iterator where, Ty&& val); [added with C++11]

    template<class... Valty>
        iterator emplace(const_iterator where, Ty&&... val); [added with C++11]

    iterator erase(const_iterator where);
    iterator erase(const_iterator first, const_iterator last);
    void clear() noexcept;

    void swap(vector& right);
    };

The template class describes an object that controls a varying-length sequence of elements of type Ty. The sequence is stored as an array of Ty.

The object allocates and frees storage for the sequence it controls through a stored allocator object of class Alloc. Such an allocator object must have the same external interface as an object of template class allocator.

Vector reallocation occurs when a member function must grow the controlled sequence beyond its current storage capacity. Other insertions and erasures may alter various storage addresses within the sequence. In all such cases, iterators or references that point at altered portions of the controlled sequence become invalid.

vector::allocator_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

vector::assign

template<class InIt>
    void assign(InIt first, InIt last);
void assign(size_type count, const Ty& val);
void assign(initializer_list<Ty> init) [added with C++11]

If InIt is an integer type, the first member function behaves the same as assign((size_type)first, (Ty)last). Otherwise, the first member function replaces the sequence controlled by *this with the sequence [first, last), which must not overlap the initial controlled sequence. The second member function replaces the sequence controlled by *this with a repetition of count elements of value val.

The third member function replaces the controlled sequence from an object of class initializer_list<Ty>.

vector::at

const_reference at(size_type off) const;
reference at(size_type off);

The member function returns a reference to the element of the controlled sequence at position off. If that position is invalid, the function throws an object of class out_of_range.

vector::back

reference back();
const_reference back() const;

The member function returns a reference to the last element of the controlled sequence, which must be non-empty.

vector::begin

const_iterator begin() const noexcept;
iterator begin() noexcept;

The member function returns a random-access iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).

vector::capacity

size_type capacity() const noexcept;

The member function returns the storage currently allocated to hold the controlled sequence, a value at least as large as size().

vector::cbegin

const_iterator cbegin() const noexcept; [added with C++11]

The member functions return a random-access iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).

vector::cend

const_reference cend() const noexcept; [added with C++11]

The member functions return a random-access iterator that points just beyond the end of the sequence.

vector::clear

void clear() noexcept;

The member function calls erase( begin(), end()).

vector::const_iterator

typedef T1 const_iterator;

The type describes an object that can serve as a constant random-access iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T1.

vector::const_pointer

typedef typename Alloc::const_pointer
    const_pointer;

The type describes an object that can serve as a constant pointer to an element of the controlled sequence.

vector::const_reference

typedef typename Alloc::const_reference
    const_reference;

The type describes an object that can serve as a constant reference to an element of the controlled sequence.

vector::const_reverse_iterator

typedef reverse_iterator<const_iterator>
    const_reverse_iterator;

The type describes an object that can serve as a constant reverse iterator for the controlled sequence.

vector::crbegin

const_reverse_iterator crbegin() const noexcept; [added with C++11]

The member functions return a reverse iterator that points just beyond the end of the controlled sequence. Hence, it designates the beginning of the reverse sequence.

vector::crend

const_reverse_iterator crend() const noexcept; [added with C++11]

The member functions return a reverse iterator that points at the first element of the sequence (or just beyond the end of an empty sequence)). Hence, it designates the end of the reverse sequence.

vector::data

Ty *data() noexcept; [added with C++11]
const Ty *data() const noexcept; [added with C++11]

The member function returns a pointer that points at the first element of the sequence, or a null pointer if the sequence is empty.

vector::difference_type

typedef typename Alloc::difference_type difference_type;

The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.

vector::emplace

template<class... Valty>
    iterator emplace(const_iterator where, Ty&&... val); [added with C++11]

The member function effectively returns insert(where, value_type(forward<Valty>(val)...)), except that the element value is constructed in place.

vector::emplace_back

template<class... Valty>
    iterator emplace_back(const_iterator where, Ty&&... val); [added with C++11]

The member function is a variadic template using rvalue references. It inserts an element with the constructor arguments val... at the end of the controlled sequence.

Inserting the element invalidates all iterators and references to existing elements.

If an exception is thrown, the container is left unaltered and the exception is rethrown.

vector::empty

bool empty() const noexcept;

The member function returns true for an empty controlled sequence.

vector::end

const_iterator end() const noexcept;
iterator end() noexcept;

The member function returns a random-access iterator that points just beyond the end of the sequence.

vector::erase

iterator erase(const_iterator where);
iterator erase(const_iterator first, const_iterator last);

The first member function removes the element of the controlled sequence pointed to by where. The second member function removes the elements of the controlled sequence in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

Erasing N elements causes N destructor calls and an assignment for each of the elements between the insertion point and the end of the sequence. No reallocation occurs, so iterators and references become invalid only from the first element erased through the end of the sequence.

The member functions throw an exception only if a copy operation throws an exception.

vector::front

reference front();
const_reference front() const;

The member function returns a reference to the first element of the controlled sequence, which must be non-empty.

vector::get_allocator

Alloc get_allocator() const noexcept;

The member function returns the stored allocator object.

vector::insert

iterator insert(const_iterator where, const Ty& val);
iterator insert(const_iterator where, size_type count, const Ty& val);
template<class InIt>
    iterator insert(const_iterator where, InIt first, InIt last);
iterator insert(const iterator where,
    initializer_list<Ty> init) [added with C++11]
iterator insert(const_iterator where, Ty&& val); [added with C++11]

Each of the member functions inserts, before the element pointed to by where in the controlled sequence, a sequence specified by the remaining operands. It then returns an iterator designating the first element in the inserted sequence, or where if the inserted sequence is empty.

The first member function inserts a single element with value val and returns an iterator that designates the newly inserted element. The second member function inserts a repetition of count elements of value val.

If InIt is an integer type, the third member function behaves the same as insert(where, (size_type)first, (Ty)last). Otherwise, the third member function inserts the sequence [first, last), which must not overlap the initial controlled sequence.

The fourth member function inserts the sequence specified by an object of class initializer_list<Ty>.

The last member function is the same as the first, but with an rvalue reference.

When inserting a single element, the number of element copies is linear in the number of elements between the insertion point and the end of the sequence. When inserting a single element at the end of the sequence, the amortized number of element copies is constant. When inserting N elements, the number of element copies is linear in N plus the number of elements between the insertion point and the end of the sequence -- except when the template member is specialized for InIt an input iterator, which behaves like N single insertions.

If reallocation occurs, the capacity increases by a fixed factor (at least), and all iterators and references become invalid. If no reallocation occurs, iterators become invalid only from the point of insertion through the end of the sequence.

If an exception is thrown during the insertion of one or more elements, and the exception is not thrown while copying an element, the container is left unaltered and the exception is rethrown.

vector::iterator

typedef T0 iterator;

The type describes an object that can serve as a random-access iterator for the controlled sequence. It is described here as a synonym for the implementation-defined type T0.

vector::max_size

size_type max_size() const noexcept;

The member function returns the length of the longest sequence that the object can control.

vector::operator=

vector& operator=(const vector& right);
vector& operator=(initializer_list<Ty> init) [added with C++11]
vector& operator=(vector&& right); [added with C++11]

The first member operator replaces the controlled sequence with a copy of the sequence controlled by right.

The second member operator replaces the controlled sequence from an object of class initializer_list<Ty>.

The third member operator is the same as the first, but with an rvalue reference.

vector::operator[]

const_reference operator[](size_type off) const;
reference operator[](size_type off);

The member function returns a reference to the element of the controlled sequence at position off. If that position is invalid, the behavior is undefined.

vector::pointer

typedef typename Alloc::pointer pointer;

The type describes an object that can serve as a pointer to an element of the controlled sequence.

vector::pop_back

void pop_back();

The member function removes the last element of the controlled sequence, which must be non-empty.

The member function never throws an exception.

vector::push_back

void push_back(const Ty& val);
void push_back(Ty&& val); [added with C++11]

The member function inserts an element with value val at the end of the controlled sequence.

Inserting the element invalidates all iterators and references to existing elements.

If an exception is thrown, the container is left unaltered and the exception is rethrown.

vector::rbegin

const_reverse_iterator rbegin() const noexcept;
reverse_iterator rbegin() noexcept;

The member function returns a reverse iterator that points just beyond the end of the controlled sequence. Hence, it designates the beginning of the reverse sequence.

vector::reference

typedef typename Alloc::reference reference;

The type describes an object that can serve as a reference to an element of the controlled sequence.

vector::rend

const_reverse_iterator rend() const noexcept;
reverse_iterator rend() noexcept;

The member function returns a reverse iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). Hence, it designates the end of the reverse sequence.

vector::reserve

void reserve(size_type count);

If count is greater than max_size(), the member function reports a length error by throwing an object of class length_error. Otherwise, it ensures that capacity() henceforth returns at least count.

vector::resize

void resize(size_type newsize);
void resize(size_type newsize, const Ty& val);

The member functions both ensure that size() henceforth returns newsize. If it must make the controlled sequence longer, the first member function appends elements with value Ty(), while the second member function appends elements with value val. To make the controlled sequence shorter, both member functions call erase(begin() + newsize, end()).

vector::reverse_iterator

typedef reverse_iterator<iterator>
    reverse_iterator;

The type describes an object that can serve as a reverse iterator for the controlled sequence.

vector::shrink_to_fit

void shrink_to_fit();

The member function eliminates any unneeded storage in the container.

vector::size

size_type size() const noexcept;

The member function returns the length of the controlled sequence.

vector::size_type

typedef typename Alloc::size_type size_type;

The unsigned integer type describes an object that can represent the length of any controlled sequence.

vector::swap

void swap(vector& right);

The member function swaps the controlled sequences between *this and right. If get_allocator() == right.get_allocator(), it does so in constant time, it throws no exceptions, and it invalidates no references, pointers, or iterators that designate elements in the two controlled sequences. Otherwise, it performs a number of element assignments and constructor calls proportional to the number of elements in the two controlled sequences.

vector::value_type

typedef typename Alloc::value_type value_type;

The type is a synonym for the template parameter Ty.

vector::vector

vector();
explicit vector(const Alloc& al);

explicit vector(size_type count);
vector(size_type count, const Ty& val);
vector(size_type count, const Ty& val,
    const Alloc& al);

vector(const vector& right);
vector(const vector& right,
    const Alloc& al); [added with C++11]
vector(vector&& right); [added with C++11]
vector(vector&& right,
    const Alloc& al); [added with C++11]

template<class InIt>
    vector(InIt first, InIt last);
template<class InIt>
    vector(InIt first, InIt last, const Alloc& al);

vector(initializer_list<Ty> init) [added with C++11]
vector(initializer_list<Ty> init,
    const Alloc& al); [added with C++11]

All constructors store an allocator object and initialize the controlled sequence. The allocator object is the argument al, if present. Otherwise, for the copy and move constructors, it is right.get_allocator(). Otherwise, it is Alloc().

The first two constructors specify an empty initial controlled sequence.

The next three constructors specify a repetition of count elements of value val, if specified. Otherwise the repeated value is Ty().

The next four constructors specify a copy of the sequence controlled by right. The last two of these constructors are the same as the first two, but with an rvalue reference.

The next two constructors specify the sequence [first, last).

The last two constructors specify the initial controlled sequence with an object of class initializer_list<Ty>.

vector<bool, Alloc>

template<class Alloc>
    class vector<bool, Alloc> {
public:
    typedef T2 pointer;
    typedef T3 const_pointer;
    class reference;
    typedef bool const_reference;

    typedef T0 iterator;
    typedef T1 const_iterator;

    void flip() noexcept;
    static void swap(reference left, reference right) noexcept;
// rest same as template class vector
    };

The class is a partial specialization of template class vector for elements of type bool. It alters the definition of four member types (to optimize the packing and unpacking of elements) and adds two member functions. Its behavior is otherwise the same as for template class vector.

vector<bool, Alloc>::const_iterator

typedef T1 const_iterator;

The type describes an object that can serve as a constant random-access iterator for the controlled sequence. It is described here as a synonym for the unspecified type T1.

vector<bool, Alloc>::const_pointer

typedef T3 const_pointer;

The type describes an object that can serve as a pointer to a constant element of the controlled sequence. It is described here as a synonym for the unspecified type T3.

vector<bool, Alloc>::const_reference

typedef bool const_reference;

The type describes an object that can serve as a constant reference to an element of the controlled sequence, in this case bool.

vector<bool, Alloc>::flip

void flip() noexcept;

The member function inverts the values of all the members of the controlled sequence.

vector<bool, Alloc>::iterator

typedef T0 iterator;

The type describes an object that can serve as a random-access iterator for the controlled sequence. It is described here as a synonym for the unspecified type T0.

vector<bool, Alloc>::pointer

typedef T2 pointer;

The type describes an object that can serve as a pointer to an element of the controlled sequence. It is described here as a synonym for the unspecified type T2.

vector<bool, Alloc>::reference

class reference {
public:
    reference() noexcept;
    reference& operator=(const reference& right) noexcept;
    reference& operator=(bool val) noexcept;
    void flip() noexcept;
    operator bool() const noexcept;
    };

The type describes an object that can serve as a reference to an element of the controlled sequence. Specifically, for two objects ref and ref2 of class reference:

It is unspecified how member functions of class vector<bool> construct objects of class reference that designate elements of a controlled sequence. The default constructor for class reference generates an object that refers to no such element.

vector<bool, Alloc>::swap

void swap(reference left, reference right) noexcept;

The static member function swaps the members of the controlled sequences designated by left and right.


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.