📄 vec_0251.htm
字号:
<HTML><TITLE>vector</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>vector</H2>
<HR><PRE> Container</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Sequence that supports random access iterators.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Special Case"><LI>Special Case</LI></A>
<A HREF="#Interface"><LI>Interface</LI></A>
<A HREF="#Constructors and Destructors"><LI>Constructors and Destructors</LI></A>
<A HREF="#Iterators"><LI>Iterators</LI></A>
<A HREF="#Assignment Operator"><LI>Assignment Operator</LI></A>
<A HREF="#Allocator"><LI>Allocator</LI></A>
<A HREF="#Reference Operators"><LI>Reference Operators</LI></A>
<A HREF="#Member Functions"><LI>Member Functions</LI></A>
<A HREF="#Non-member Operators"><LI>Non-member Operators</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <vector></PRE>
<PRE>
template <class T, class Allocator = allocator>
class <B>vector</B> ;
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P><B><I>vector<T, Allocator></B></I> 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 <B><I>vector</B></I>, <SAMP>iterator</SAMP> is a random access iterator referring to <SAMP>T</SAMP>. <SAMP>const_iterator</SAMP> is a constant random access iterator that returns a <SAMP>const T&</SAMP> when being dereferenced. A constructor for <SAMP>iterator</SAMP> and <SAMP>const_iterator</SAMP> is guaranteed. <SAMP>size_type</SAMP> is an unsigned integral type. <SAMP>difference_type</SAMP> is a signed integral type. </P>
<P>Any type used for the template parameter <SAMP>T</SAMP> must provide the following (where <SAMP>T</SAMP> is the <SAMP>type</SAMP>, <SAMP>t</SAMP> is a <SAMP>value</SAMP> of <SAMP>T</SAMP> and <SAMP>u</SAMP> is a <SAMP>const</SAMP> <SAMP>value</SAMP> of <SAMP>T</SAMP>): </P>
<PRE>Default constructor T()
Copy constructors T(t) and T(u)
Destructor t.~T()
Address of &t and &u yielding T* and
const T* respectively
Assignment t = a where a is a
(possibly const) value of T
</PRE>
<A NAME="Special Case"><H3>Special Case</H3></A>
<P>Vectors of bit values (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, <B><I>vector<bool></B></I>, are a superset of those for an ordinary vector, only the implementation is more efficient.</P>
<P>Two member functions are available to the the boolean vector data type. One is <SAMP>flip()</SAMP>, which inverts all the bits of the vector. Boolean vectors also return as reference an internal value that also supports the <SAMP>flip()</SAMP> member function. The other <B><I>vector<bool></B></I>-specific member function is a second form of the <SAMP>swap()</SAMP> function</P>
<A NAME="Interface"><H3>Interface</H3></A>
<PRE>template <class T, class Allocator = allocator>
class vector {
public:
// Types
typedef T value_type;
typedef Allocator allocator_type;
typename reference;
typename const_reference;
typename iterator;
typename const_iterator;
typename size_type;
typename difference_type;
typename reverse_iterator;
typename 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);
template <class Size, class TT>
void assign (Size n);
template <class Size, class TT>
void assign (Size n, const TT&);
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);
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>&);
};
// 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>&);
// Specialized Algorithms
template <class T, class Allocator>
void swap (const vector<T,Allocator>&, const vector<T,Allocator>&);
</PRE>
<A NAME="Constructors and Destructors"><H3>Constructors and Destructors</H3></A>
<PRE>explicit <B>vector </B>(const Allocator& alloc = Allocator());</PRE>
<UL><P>The default constructor. Creates a vector of length zero. The vector will use the allocator <SAMP>alloc</SAMP> for all storage management.</P>
</UL>
<PRE>explicit <B>vector</B> (size_type n,
const Allocator& alloc = Allocator());</PRE>
<UL><P>Creates a vector of length <SAMP>n</SAMP>, containing <SAMP>n</SAMP> copies of the default value for type <SAMP>T</SAMP>. Requires that <SAMP>T</SAMP> have a default constructor. The vector will use the allocator <SAMP>alloc</SAMP> for all storage management.</P>
</UL>
<PRE><B>vector</B> (size_type n, const T& value,
const Allocator& alloc = Allocator());</PRE>
<UL><P>Creates a vector of length <SAMP>n</SAMP>, containing <SAMP>n</SAMP> copies of value. The vector will use the allocator <SAMP>alloc</SAMP> for all storage management.</P>
</UL>
<PRE><B>vector</B> (const vector<T, Allocator>& x);</PRE>
<UL><P>Creates a copy of <SAMP>x</SAMP>.</P>
</UL>
<PRE>template <class InputIterator>
<B>vector</B> (InputIterator first, InputIterator last,
const Allocator& alloc = Allocator());</PRE>
<UL><P>Creates a vector of length <SAMP>last - first</SAMP>, filled with all values obtained by dereferencing the <SAMP>InputIterators</SAMP> on the range <SAMP>[first, last)</SAMP>. The vector will use the allocator <SAMP>alloc</SAMP> for all storage management.</P>
</UL>
<PRE><B>~vector</B> ();</PRE>
<UL><P>The destructor. Releases any allocated memory for this vector.</P>
</UL>
<A NAME="Iterators"><H3>Iterators</H3></A>
<PRE>iterator <B>begin</B> ();</PRE>
<UL><P>Returns a random access <SAMP>iterator</SAMP> that points to the first element.</P>
</UL>
<PRE>const_iterator <B>begin</B> () const;</PRE>
<UL><P>Returns a random access <SAMP>const_iterator</SAMP> that points to the first element.</P>
</UL>
<PRE>iterator <B>end</B> ();</PRE>
<UL><P>Returns a random access <SAMP>iterator</SAMP> that points to the past-the-end value.</P>
</UL>
<PRE>const_iterator <B>end</B> () const;</PRE>
<UL><P>Returns a random access <SAMP>const_iterator</SAMP> that points to the past-the-end value.</P>
</UL>
<PRE>reverse_iterator <B>rbegin</B> ();</PRE>
<UL><P>Returns a random access <SAMP>reverse_iterator</SAMP> that points to the past-the-end value.</P>
</UL>
<PRE>const_reverse_iterator <B>rbegin</B> () const;</PRE>
<UL><P>Returns a random access <SAMP>const_reverse_iterator</SAMP> that points to the past-the-end value.</P>
</UL>
<PRE>reverse_iterator <B>rend</B> ();</PRE>
<UL><P>Returns a random access <SAMP>reverse_iterator</SAMP> that points to the first element.</P>
</UL>
<PRE>const_reverse_iterator <B>rend</B> () const;</PRE>
<UL><P>Returns a random access <SAMP>const_reverse_iterator</SAMP> that points to the first element.</P>
</UL>
<A NAME="Assignment Operator"><H3>Assignment Operator</H3></A>
<PRE>vector<T, Allocator>& <B>operator= </B>(const vector<T, Allocator>&
x);</PRE>
<UL><P>Erases all elements in self then inserts into self a copy of each element in <SAMP>x</SAMP>. Returns a reference to self.</P>
</UL>
<A NAME="Allocator"><H3>Allocator</H3></A>
<PRE>allocator_type <B>get_allocator</B> () const;</PRE>
<UL><P>Returns a copy of the allocator used by self for storage management.</P>
</UL>
<A NAME="Reference Operators"><H3>Reference Operators</H3></A>
<PRE>reference <B>operator[]</B> (size_type n);</PRE>
<UL><P>Returns a reference to element <SAMP>n</SAMP> of self. The result can be used as an lvalue. The index <SAMP>n</SAMP> must be between 0 and the <SAMP>size</SAMP> less one.</P>
</UL>
<PRE>const_reference <B>operator[]</B> (size_type n) const;</PRE>
<UL><P>Returns a constant reference to element <SAMP>n</SAMP> of self. The index <SAMP>n</SAMP> must be between 0 and the <SAMP>size</SAMP> less one.</P>
</UL>
<A NAME="Member Functions"><H3>Member Functions</H3></A>
<PRE>template <class InputIterator>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -