.\" ident @(#)vector.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH vector 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2vector\fP \ - A sequence that supports random access iterators. .SH SYNOPSIS .br #include .br template > .br class vector ; .SH DESCRIPTION vector 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, \f2iterator\fP is a random access iterator referring to \f2T\fP. \f2const_iterator\fP is a constant random access iterator that returns a \f2const T&\fP when dereferenced. A constructor for \f2iterator\fP and \f2const_iterator\fP is guaranteed. \f2size_type\fP is an unsigned integral type. \f2difference_type\fP is a signed integral type. Any type used for the template parameter \f2T\fP must provide the following (where \f2T\fP is the \f2type\fP, \f2t\fP is a \f2value\fP of \f2T\fP and \f2u\fP is a \f2const\fP \f2value\fP of \f2T\fP): .br .RE .RS 1 Copy constructors T(t) and T(u) .br Destructor t.~T() .br Address of &t and &u yielding T* and const T* .RE .RS 23 respectively .RE .RS 1 Assignment t = a where a is a .RE .RS 24 (possibly const) value of T .SH 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, 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 \f2flip()\fP, which inverts all the bits of the vector. Boolean vectors also return as reference an internal value that also supports the \f2flip()\fP member function. The other vector-specific member function is a second form of the \f2swap()\fP function .SH INTERFACE .RE .RS 0 template > .br class vector { .br public: .br // Types .br typedef T value_type; .br typedef Allocator allocator_type; .br typedef typename Allocator::reference reference; .br typedef typename Allocator::const_reference const_reference; .br class iterator; .br class const_iterator; .br typedef typename Allocator::size_type size_type; .br typedef typename Allocator::difference_type difference_type; .br typedef typename std::reverse_iterator .RE .RS 16 reverse_iterator; .RE .RS 0 typedef typename std::reverse_iterator .RE .RS 16 const_reverse_iterator; .RE .RS 0 .br // Construct/Copy/Destroy .br explicit vector (const Allocator& = Allocator()); .br explicit vector (size_type, const Allocator& = Allocator ()); .br vector (size_type, const T&, const Allocator& = Allocator()); .br vector (const vector&); .br template .br vector (InputIterator, InputIterator, .br const Allocator& = Allocator ()); .br ~vector (); .br vector& operator= (const vector&); .br template .br void assign (InputIterator first, InputIterator last); .br void assign (size_type, const); .br allocator_type get_allocator () const; .br // Iterators .br iterator begin (); .br const_iterator begin () const; .br iterator end (); .br const_iterator end () const; .br reverse_iterator rbegin (); .br const_reverse_iterator rbegin () const; .br reverse_iterator rend (); .br const_reverse_iterator rend () const; .br .br // Capacity .br size_type size () const; .br size_type max_size () const; .br void resize (size_type); .br void resize (size_type, T); .br size_type capacity () const; .br bool empty () const; .br void reserve (size_type); .br .br // Element Access .br reference operator[] (size_type); .br const_reference operator[] (size_type) const; .br reference at (size_type); .br const_reference at (size_type) const; .br reference front (); .br const_reference front () const; .br reference back (); .br const_reference back () const; .br .br // Modifiers .br void push_back (const T&); .br void pop_back (); .br iterator insert (iterator, const T&); .br void insert (iterator, size_type, const T&); .br template .br void insert (iterator, InputIterator, InputIterator); .br iterator erase (iterator); .br iterator erase (iterator, iterator); .br void swap (vector&); .br void clear() .br }; .br .br // Non-member Operators .br template .br bool operator== (const vector&, .br const vector &); .br template .br bool operator!= (const vector&, .br const vector &); .br template .br bool operator< (const vector&, .br const vector&); .br template .br bool operator> (const vector&, .br const vector&); .br template .br bool operator<= (const vector&, .br const vector&); .br template .br bool operator>= (const vector&, .br const vector&); .br .br // Specialized Algorithms .br template .br void swap (const vector&, const vector&); .SH CONSTRUCTORS .br explicit vector(const Allocator& alloc = Allocator()); .RE .RS 3 The default constructor. Creates a vector of length zero. The vector will use the allocator \f2alloc\fP for all storage management. .RE .br explicit vector(size_type n); .RE .RS 3 Creates a vector of length \f2n\fP, containing \f2n\fP copies of the default value for type \f2T\fP. Requires that \f2T\fP have a default constructor. The vector will use the allocator \f2Allocator()\fP for all storage management. .RE .br vector(size_type n, const T& value, .br const Allocator& alloc = Allocator()); .RE .RS 3 Creates a vector of length \f2n\fP, containing \f2n\fP copies of value. The vector will use the allocator \f2alloc\fP for all storage management. .RE .br vector(const vector& x); .RE .RS 3 Creates a copy of \f2x\fP. .RE .br template .br vector(InputIterator first, InputIterator last, .br const Allocator& alloc = Allocator()); .RE .RS 3 Creates a vector of length \f2last - first\fP, filled with all values obtained by dereferencing the \f2InputIterators\fP on the range \f2[first, last)\fP. The vector will use the allocator \f2alloc\fP for all storage management. .RE .SH DESTRUCTOR .br ~vector(); .RE .RS 3 The destructor. Releases any allocated memory for this vector. .RE .SH ITERATORS .br iterator .br begin(); .RE .RS 3 Returns a random access \f2iterator\fP that points to the first element. .RE .br const_iterator .br begin() const; .RE .RS 3 Returns a random access \f2const_iterator\fP that points to the first element. .RE .br iterator .br end(); .RE .RS 3 Returns a random access \f2iterator\fP that points to the past-the-end value. .RE .br const_iterator .br end() const; .RE .RS 3 Returns a random access \f2const_iterator\fP that points to the past-the-end value. .RE .br reverse_iterator .br rbegin(); .RE .RS 3 Returns a random access \f2reverse_iterator\fP that points to the past-the-end value. .RE .br const_reverse_iterator .br rbegin() const; .RE .RS 3 Returns a random access \f2const_reverse_iterator\fP that points to the past-the-end value. .RE .br reverse_iterator .br rend(); .RE .RS 3 Returns a random access \f2reverse_iterator\fP that points to the first element. .RE .br const_reverse_iterator .br rend() const; .RE .RS 3 Returns a random access \f2const_reverse_iterator\fP that points to the first element. .RE .SH ASSIGNMENT OPERATOR .br vector& .br operator=(const vector& x); .RE .RS 3 Erases all elements in self then inserts into self a copy of each element in \f2x\fP. Returns a reference to self. .RE .SH ALLOCATOR .br allocator_type .br get_allocator() const; .RE .RS 3 Returns a copy of the allocator used by self for storage management. .RE .SH REFERENCE OPERATORS .br reference .br operator[](size_type n); .RE .RS 3 Returns a reference to element \f2n\fP of self. The result can be used as an lvalue. The index \f2n\fP must be between 0 and the \f2size\fP less one. .RE .br const_reference .br operator[](size_type n) const; .RE .RS 3 Returns a constant reference to element \f2n\fP of self. The index \f2n\fP must be between 0 and the \f2size\fP less one. .RE .SH MEMBER FUNCTIONS .br template .br void .br assign(InputIterator first, InputIterator last); .RE .RS 3 Erases all elements contained in self, then inserts new elements from the range \f2[first, last)\fP. .RE .br void .br assign(size_type, const T& t); .RE .RS 3 Erases all elements contained in self, then inserts \f2n\fP instances of the value of \f2t\fP. .RE .br reference .br at(size_type n); .RE .RS 3 Returns a reference to element \f2n\fP of self. The result can be used as an lvalue. The index \f2n\fP must be between 0 and the \f2size\fP less one. .RE .br const_reference .br at(size_type) const; .RE .RS 3 Returns a constant reference to element \f2n\fP of self. The index \f2n\fP must be between 0 and the \f2size\fP less one. .RE .br reference .br back(); .RE .RS 3 Returns a reference to the last element. .RE .br const_reference .br back() const; .RE .RS 3 Returns a constant reference to the last element. .RE .br size_type .br capacity() const; .RE .RS 3 Returns the size of the allocated storage, as the number of elements that can be stored. .RE .br void .br clear() ; .RE .RS 3 Deletes all elements from the vector. .RE .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the \f2size\fP is zero. .RE .br iterator .br erase(iterator position); .RE .RS 3 Deletes the vector element pointed to by the iterator \f2position\fP. Returns an \f2iterator\fP pointing to the element following the deleted element, or \f2end()\fP if the deleted element was the last one in this vector. .RE .br iterator .br erase(iterator first, iterator last); .RE .RS 3 Deletes the vector elements in the range (first, last). Returns an \f2iterator\fP pointing to the element following the last deleted element, or \f2end()\fP if there were no elements in the deleted range. .RE .br void .br flip(); .RE .RS 3 Flips all the bits in the vector. This member function is only defined for vector. .RE .br reference .br front(); .RE .RS 3 Returns a reference to the first element. .RE .br const_reference .br front() const; .RE .RS 3 Returns a constant reference to the first element. .RE .br iterator .br insert(iterator position, const T& x); .RE .RS 3 Inserts \f2x\fP before \f2position\fP. The return value points to the inserted \f2x\fP. .RE .br void .br insert(iterator position, size_type n, const T& x); .RE .RS 3 Inserts \f2n\fP copies of \f2x\fP before \f2position\fP. .RE .br template .br void .br insert(iterator position, InputIterator first, .br InputIterator last); .RE .RS 3 Inserts copies of the elements in the range \f2[first, last]\fP before \f2position\fP. .RE .br size_type .br max_size() const; .RE .RS 3 Returns \f2size()\fP of the largest possible vector. .RE .br void .br pop_back(); .RE .RS 3 Removes the last element of self. .RE .br void .br push_back(const T& x); .RE .RS 3 Inserts a copy of \f2x\fP to the end of self. .RE .br void .br reserve(size_type n); .RE .RS 3 Increases the capacity of self in anticipation of adding new elements. \f2reserve\fP itself does not add any new elements. After a call to \f2reserve,\fP \f2capacity()\fP is greater than or equal to \f2n\fP and subsequent insertions will not cause a reallocation until the size of the vector exceeds \f2n\fP. Reallocation does not occur if \f2n\fP is less than \f2capacity()\fP. If reallocation does occur, then all iterators and references pointing to elements in the vector are invalidated. \f2reserve\fP takes at most linear time in the size of self. \f2reserve\fP throws a \f2length_error\fP exception if \f2n\fP is greater than \f2max_size()\fP. .RE .br void .br resize(size_type sz); .RE .RS 3 Alters the size of self. If the new size (\f2sz\fP) is greater than the current size, then \f2sz-size()\fP instances of the default value of type\f2 T \fP are inserted at the end of the vector. If the new size is smaller than the current \f2capacity\fP, then the vector is truncated by erasing \f2size()-sz\fP elements off the end. If \f2sz\fP is equal to \f2capacity\fP then no action is taken. .RE .br void .br resize(size_type sz, T c); .RE .RS 3 Alters the size of self. If the new size (\f2sz\fP) is greater than the current size, then \f2sz-size()\fP \f2c\fP's are inserted at the end of the vector. If the new size is smaller than the current \f2capacity\fP, then the vector is truncated by erasing \f2size()-sz\fP elements off the end. If \f2sz\fP is equal to \f2capacity\fP then no action is taken. .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements. .RE .br void .br swap(vector& x); .RE .RS 3 Exchanges self with \f2x\fP, by swapping all elements. .RE .br void .br swap(reference x, reference y); .RE .RS 3 Swaps the values of \f2x\fP and \f2y\fP. This is a member function of vector only. .RE .SH NON-MEMBER OPERATORS .br template .br bool operator==(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is the same as \f2y\fP. .RE .br template .br bool operator!=(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .br template .br bool operator<(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2true\fP if the elements contained in \f2x \fPare lexicographically less than the elements contained in \f2y\fP. .RE .br template .br bool operator>(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2y < x\fP. .RE .br template .br bool operator<=(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .br template .br bool operator>=(const vector& x, .br const vector& y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH SPECIALIZED ALGORITHMS .br template void .br swap(vector & a, vector & b); .RE .RS 3 Efficiently swaps the contents of \f2a\fP and \f2b\fP. .RE .SH EXAMPLE .br // .br // vector.cpp .br // .br #include .br #include .br .br ostream& operator<< (ostream& out, .br const vector& v) .RE .RS 1 { .br copy(v.begin(),v.end(),ostream_iterator(out," ")); .br return out; .br } .RE .RS 0 int main(void) .RE .RS 1 { .RE .RS 0 // create a vector of doubles .RE .RS 3 vector vi; .RE .RS 2 int i; .RE .RS 0 .br for(i = 0; i < 10; ++i) { .RE .RS 3 // insert values before the beginning .RE .RS 2 vi.insert(vi.begin(), i); .RE .RS 3 } .RE .RS 0 .br // print out the vector .br cout << vi << endl; .br .br // now let's erase half of the elements .br int half = vi.size() >> 1; .br for(i = 0; i < half; ++i) { .RE .RS 2 vi.erase(vi.begin()); .RE .RS 3 } .RE .RS 0 .br // print ir out again .br cout << vi << endl; .br .br return 0; .RE .RS 1 } .RE .RS 0 .br Output : .br 9 8 7 6 5 4 3 2 1 0 .br 4 3 2 1 0 .br .SH 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 that takes two templated iterators: .br .br template .br vector (InputIterator, InputIterator, .br const Allocator = Allocator()); 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: .br .br int intarray[10]; .br vector first_vector(intarray, intarray + 10); .br vector second_vector(first_vector.begin(), .br first_vector.end()); but not this way: .br .br vector .br long_vector(first_vector.begin(),first_vector.end()); since the \f2long_vector\fP and \f2first_vector\fP are not the same type. Additionally, if your compiler does not support default template parameters, you will need to supply the \f2Allocator\fP template argument. For instance, you will need to write : .br \f2\fP .br \f2vector >\fP instead of : .br \f2\fP .br \f2vector\fP .SH SEE ALSO allocator, Containers, Iterators, lexicographical_compare