📄 vec_0251.htm
字号:
void
<B>assign</B> (InputIterator first, InputIterator last);</PRE>
<UL><P>Erases all elements contained in self, then inserts new elements from the range <SAMP>[first, last)</SAMP>.</P>
</UL>
<PRE>template <class Size, class T>
void
<B>assign</B> (Size n, const T& t); </PRE>
<UL><P>Erases all elements contained in self, then inserts <SAMP>n</SAMP> instances of the default value of type <SAMP>T</SAMP>.</P>
</UL>
<PRE>template <class Size, class T>
void
<B>assign</B> (Size n, const T& t); </PRE>
<UL><P>Erases all elements contained in self, then inserts <SAMP>n</SAMP> instances of the value of <SAMP>t</SAMP>.</P>
</UL>
<PRE>reference
<B>at</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>at</B> (size_type) 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>
<PRE>reference
<B>back</B> ();</PRE>
<UL><P>Returns a reference to the last element.</P>
</UL>
<PRE>const_reference
<B>back</B> () const;</PRE>
<UL><P>Returns a constant reference to the last element.</P>
</UL>
<PRE>size_type
<B>capacity</B> () const;</PRE>
<UL><P>Returns the size of the allocated storage, as the number of elements that can be stored.</P>
</UL>
<PRE>void <B>clear</B> () ; </PRE>
<UL><P> Deletes all elements from the vector.</P>
</UL>
<PRE>bool
<B>empty</B> () const;</PRE>
<UL><P> Returns <SAMP>true</SAMP> if the <SAMP>size</SAMP> is zero.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator position);</PRE>
<UL><P>Deletes the vector element pointed to by the iterator <SAMP>position</SAMP>. Returns an <SAMP>iterator</SAMP> pointing to the element following the deleted element, or <SAMP>end()</SAMP> if the deleted element was the last one in this vector. </P>
</UL>
<PRE>
iterator
<B>erase</B> (iterator first, iterator last);</PRE>
<UL><P>Deletes the vector elements in the range (first, last). Returns an <SAMP>iterator</SAMP> pointing to the element following the last deleted element, or <SAMP>end()</SAMP> if there were no elements in the deleted range. </P>
</UL>
<PRE>
void
<B>flip</B>();</PRE>
<UL><P>Flips all the bits in the vector. <I>This member function is only defined for </I><B><I>vector<bool></B></I>.</P>
</UL>
<PRE>reference
<B>front</B> ();</PRE>
<UL><P>Returns a reference to the first element.</P>
</UL>
<PRE>const_reference
<B>front</B> () const;</PRE>
<UL><P>Returns a constant reference to the first element.</P>
</UL>
<PRE>iterator
<B>insert</B> (iterator position);</PRE>
<UL><P>Inserts <SAMP>x</SAMP> before <SAMP>position</SAMP>. The return value points to the inserted <SAMP>x</SAMP>.</P>
</UL>
<PRE>iterator
<B>insert</B> (iterator position, const T& x);</PRE>
<UL><P>Inserts <SAMP>x</SAMP> before <SAMP>position</SAMP>. The return value points to the inserted <SAMP>x</SAMP>.</P>
</UL>
<PRE>void
<B>insert</B> (iterator position, size_type n, const T& x);</PRE>
<UL><P>Inserts <SAMP>n</SAMP> copies of <SAMP>x</SAMP> before <SAMP>position</SAMP>.</P>
</UL>
<PRE>template <class InputIterator>
void
<B>insert</B> (iterator position, InputIterator first,
InputIterator last);</PRE>
<UL><P>Inserts copies of the elements in the range <SAMP>[first, last]</SAMP> before <SAMP>position</SAMP>.</P>
</UL>
<PRE>size_type
<B>max_size</B> () const;</PRE>
<UL><P>Returns <SAMP>size()</SAMP> of the largest possible vector.</P>
</UL>
<PRE>void
<B>pop_back</B> ();</PRE>
<UL><P>Removes the last element of self.</P>
</UL>
<PRE>void
<B>push_back</B> (const T& x);</PRE>
<UL><P>Inserts a copy of <SAMP>x</SAMP> to the end of self.</P>
</UL>
<PRE>void
<B>reserve</B> (size_type n);</PRE>
<UL><P>Increases the capacity of self in anticipation of adding new elements. <SAMP>reserve</SAMP> itself does not add any new elements. After a call to <SAMP>reserve,</SAMP> <SAMP>capacity()</SAMP> is greater than or equal to <SAMP>n</SAMP> and subsequent insertions will not cause a reallocation until the size of the vector exceeds <SAMP>n</SAMP>. Reallocation does not occur if <SAMP>n</SAMP> is less than <SAMP>capacity()</SAMP>. If reallocation does occur, then all iterators and references pointing to elements in the vector are invalidated. <SAMP>reserve</SAMP> takes at most linear time in the size of self.</P>
</UL>
<PRE>void
<B>resize</B> (size_type sz);</PRE>
<UL><P>Alters the size of self. If the new size (<SAMP>sz</SAMP>) is greater than the current size, then <SAMP>sz-size()</SAMP> instances of the default value of type<SAMP> T </SAMP> are inserted at the end of the vector. If the new size is smaller than the current <SAMP>capacity</SAMP>, then the vector is truncated by erasing <SAMP>size()-sz</SAMP> elements off the end. If <SAMP>sz</SAMP> is equal to <SAMP>capacity</SAMP> then no action is taken.</P>
</UL>
<PRE>void
<B>resize</B> (size_type sz, T c);</PRE>
<UL><P>Alters the size of self. If the new size (<SAMP>sz</SAMP>) is greater than the current size, then <SAMP>sz-size()</SAMP> <SAMP>c</SAMP>'s are inserted at the end of the vector. If the new size is smaller than the current <SAMP>capacity</SAMP>, then the vector is truncated by erasing <SAMP>size()-sz</SAMP> elements off the end. If <SAMP>sz</SAMP> is equal to <SAMP>capacity</SAMP> then no action is taken.</P>
</UL>
<PRE>size_type
<B>size</B> () const;</PRE>
<UL><P>Returns the number of elements.</P>
</UL>
<PRE>void
<B>swap</B> (vector<T, Allocator>& x);</PRE>
<UL><P>Exchanges self with <SAMP>x</SAMP>, by swapping all elements.</P>
</UL>
<PRE>
void
<B>swap</B>(reference x, reference y);</PRE>
<UL><P>Swaps the values of <SAMP>x</SAMP> and <SAMP>y</SAMP>. <I>This is a member function of</I> <B><I>vector<bool></B></I> <I>only</I>.</P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>template <class T, class Allocator>
bool <B>operator==</B> (const vector<T, Allocator>& x,
const vector<T, Allocator>& y);</PRE>
<UL><P>Returns <SAMP>true</SAMP> if <SAMP>x</SAMP> is the same as <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class T>
bool <B>operator<</B> (const vector<T, Allocator>& x,
const vector<T, Allocator>& y);</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the elements contained in <SAMP>x </SAMP>are lexicographically less than the elements contained in <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class T, class Allocator>
void <B>swap </B>(vector <T, Allocator>& a, vector <T, Allocator>& b);</PRE>
<UL><P>Efficiently swaps the contents of <SAMP>a</SAMP> and <SAMP>b</SAMP>.</P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// vector.cpp
//
#include <vector>
#include <iostream.h></PRE><PRE> ostream& operator<< (ostream& out,
const vector<int, allocator>& v)
{
copy(v.begin(), v.end(), ostream_iterator<int>(out," "));
return out;
}</PRE><PRE> int main(void)
{
// create a vector of doubles
<B>vector</B><int, allocator> vi;
int i;</PRE><PRE> for(i = 0; i < 10; ++i) {
// insert values before the beginning
vi.insert(vi.begin(), i);
}</PRE><PRE> // print out the vector
cout << vi << endl;</PRE>
<PRE> // now let's erase half of the elements
int half = vi.size() >> 1;</PRE>
<PRE> for(i = 0; i < half; ++i) {
vi.erase(vi.begin());
}</PRE><PRE> // print ir out again
cout << vi << endl;</PRE>
<PRE> return 0;</PRE>
<PRE> }</PRE>
<PRE>Output :
9 8 7 6 5 4 3 2 1 0
4 3 2 1 0
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for <B><I>vector<T, Allocator></B></I> that takes two templated iterators:</P>
<PRE>template <class InputIterator>
vector (InputIterator, InputIterator,
const Allocator = Allocator());
</PRE>
<P><B><I>vector</B></I> 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.</P>
<P>For example, if your compiler does not support member function templates you can construct a vector in the following two ways:</P>
<PRE>int intarray[10];
vector<int, allocator> first_vector(intarray, intarray + 10);
vector<int, allocator> second_vector(first_vector.begin(),
first_vector.end());
</PRE>
<P>but not this way:</P>
<PRE>vector<long, allocator>
long_vector(first_vector.begin(),first_vector.end());
</PRE>
<P>since the <SAMP>long_vector</SAMP> and <SAMP>first_vector</SAMP> are not the same type.</P>
<P>Additionally, if your compiler does not support default template parameters, you will need to supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="all_7029.htm"><B><I>allocator</B></I></A>, <A HREF="Con_2487.htm"><B><I>Containers</B></I></A>, <A HREF="Ite_5295.htm"><B><I>Iterators</B></I></A>, <A HREF="lex_3806.htm"><B><I>lexicographical_compare</B></I></A></P>
<HR>
<A HREF="val_9388.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="wst_4531.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -