Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

vector


Container

Summary

A sequence that supports random access iterators.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <vector>
template <class T, class Allocator = allocator<T> >
class vector ;

Description

vector<T, Allocator> is a type of sequence that supports random access iterators. In addition, it supports amortized constant time insert and erase operations at the end. Insert and erase in the middle take linear time. Storage management is handled automatically. In vector, iterator is a random access iterator referring to T. const_iterator is a constant random access iterator that returns a const T& when dereferenced. A constructor for iterator and const_iterator is guaranteed. size_type is an unsigned integral type. difference_type is a signed integral type.

Any type used for the template parameter T must provide the following (where T is the type, t is a value of T and u is a const value of T):

Special Case

Vectors of bit values, that is boolean 1/0 values, are handled as a special case by the standard library, so that they can be efficiently packed several elements to a word. The operations for a boolean vector, vector<bool>, are a superset of those for an ordinary vector, only the implementation is more efficient.

Two member functions are available to the boolean vector data type. One is flip(), which inverts all the bits of the vector. Boolean vectors also return as reference an internal value that also supports the flip() member function. The other vector<bool>-specific member function is a second form of the swap() function

Interface

template <class T, class Allocator = allocator<T> >
class vector {
public:
// Types
typedef T value_type;
typedef Allocator allocator_type;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference; 
class iterator;
class const_iterator;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename std::reverse_iterator<iterator>
                 reverse_iterator;
typedef typename std::reverse_iterator<const iterator>
                 const_reverse_iterator;

// Construct/Copy/Destroy
explicit vector (const Allocator& = Allocator());
explicit vector (size_type, const Allocator& = Allocator ());
vector (size_type, const T&, const Allocator& = Allocator());
vector (const vector<T, Allocator>&);
template <class InputIterator>
vector (InputIterator, InputIterator, 
const Allocator& = Allocator ());
~vector ();
vector<T,Allocator>& operator= (const vector<T, Allocator>&);
template <class InputIterator>
void assign (InputIterator first, InputIterator last);
void assign (size_type, const);
allocator_type get_allocator () const;
// Iterators
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;

// Capacity
size_type size () const;
size_type max_size () const;
void resize (size_type);
void resize (size_type, T);
size_type capacity () const;
bool empty () const;
void reserve (size_type);

// Element Access
reference operator[] (size_type);
const_reference operator[] (size_type) const;
reference at (size_type);
const_reference at (size_type) const;
reference front ();
const_reference front () const;
reference back ();
const_reference back () const;

// Modifiers
void push_back (const T&);
void pop_back ();
iterator insert (iterator, const T&);
void insert (iterator, size_type, const T&);
template <class InputIterator>
void insert (iterator, InputIterator, InputIterator);
iterator erase (iterator);
iterator erase (iterator, iterator);
void swap (vector<T, Allocator>&);
void clear()
};

// Non-member Operators
template <class T>
bool operator== (const vector<T,Allocator>&, 
const vector <T,Allocator>&);
template <class T>
bool operator!= (const vector<T,Allocator>&, 
const vector <T,Allocator>&);
template <class T>
bool operator< (const vector<T,Allocator>&, 
const vector<T,Allocator>&);
template <class T>
bool operator> (const vector<T,Allocator>&, 
const vector<T,Allocator>&);
template <class T>
bool operator<= (const vector<T,Allocator>&, 
const vector<T,Allocator>&);
template <class T>
bool operator>= (const vector<T,Allocator>&, 
const vector<T,Allocator>&);

// Specialized Algorithms
template <class T, class Allocator>
void swap (const vector<T,Allocator>&, const vector<T,Allocator>&);

Constructors

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

Destructor

~vector();

Iterators

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;

Assignment Operator

vector<T, Allocator>& 
operator=(const vector<T, Allocator>& x);

Allocator

allocator_type 
get_allocator() const;

Reference Operators

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

Member Functions

template <class InputIterator>
void 
assign(InputIterator first, InputIterator last);
void 
assign(size_type, const T& t); 
reference 
at(size_type n);
const_reference 
at(size_type) const;
reference 
back();
const_reference 
back() const;
size_type 
capacity() const;
void 
clear() ; 
bool 
empty() const;
iterator
erase(iterator position);
iterator
erase(iterator first, iterator last);
void
flip();
reference 
front();
const_reference 
front() const;
iterator 
insert(iterator position, const T& x);
void 
insert(iterator position, size_type n, const  T& x);
template <class InputIterator>
void 
insert(iterator position, InputIterator first, 
InputIterator last);
size_type 
max_size() const;
void 
pop_back();
void 
push_back(const T& x);
void 
reserve(size_type n);
void 
resize(size_type sz);
void 
resize(size_type sz, T c);
size_type 
size() const;
void 
swap(vector<T, Allocator>& x);
void
swap(reference x, reference y);

Non-member Operators

template <class T, class Allocator>
bool operator==(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);
template <class T, class Allocator>
bool operator!=(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);
template <class T>
bool operator<(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);
template <class T>
bool operator>(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);
template <class T>
bool operator<=(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);
template <class T>
bool operator>=(const vector<T, Allocator>& x,
const vector<T, Allocator>& y);

Specialized Algorithms

template <class T, class Allocator>void 
swap(vector <T, Allocator>& a, vector <T, Allocator>& b);

Example

Warnings

Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for vector<T, Allocator> that takes two templated iterators:

vector also has an insert function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature we provide substitute functions that allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on), or you can use a pointer to the type of element you have in the container.

For example, if your compiler does not support member function templates you can construct a vector in the following two ways:

but not this way:

since the long_vector and first_vector are not the same type.

Additionally, if your compiler does not support default template parameters, you will need to supply the Allocator template argument. For instance, you will need to write :

instead of :

See Also

allocator, Containers, Iterators, lexicographical_compare



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998