📄 container.html
字号:
</TH></TR><TR><TD VAlign=top>Copy constructor</TD><TD VAlign=top><tt>X(a)</tt></TD><TD VAlign=top> </TD><TD VAlign=top> </TD><TD VAlign=top><tt>X().size() == a.size()</tt>. <tt>X()</tt> contains a copy of each of <tt>a</tt>'s elements.</TD></TR><TR><TD VAlign=top>Copy constructor</TD><TD VAlign=top><tt>X b(a);</tt></TD><TD VAlign=top> </TD><TD VAlign=top> </TD><TD VAlign=top><tt>b.size() == a.size()</tt>. <tt>b</tt> contains a copy of each of <tt>a</tt>'s elements.</TD></TR><TR><TD VAlign=top>Assignment operator</TD><TD VAlign=top><tt>b = a</tt></TD><TD VAlign=top> </TD><TD VAlign=top> </TD><TD VAlign=top><tt>b.size() == a.size()</tt>. <tt>b</tt> contains a copy of each of <tt>a</tt>'s elements.</TD></TR><TR><TD VAlign=top>Destructor</TD><TD VAlign=top><tt>a.~X()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Each of <tt>a</tt>'s elements is destroyed, and memory allocated for them (if any) is deallocated.</TD><TD VAlign=top> </TD></TR><TR><TD VAlign=top>Beginning of range</TD><TD VAlign=top><tt>a.begin()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Returns an iterator pointing to the first element in the container. <A href="#7">[7]</A></TD><TD VAlign=top><tt>a.begin()</tt> is either dereferenceable or past-the-end. It is past-the-end if and only if <tt>a.size() == 0</tt>.</TD></TR><TR><TD VAlign=top>End of range</TD><TD VAlign=top><tt>a.end()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Returns an iterator pointing one past the last element in the container.</TD><TD VAlign=top><tt>a.end()</tt> is past-the-end.</TD></TR><TR><TD VAlign=top>Size</TD><TD VAlign=top><tt>a.size()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Returns the size of the container, that is, its number of elements. <A href="#8">[8]</A></TD><TD VAlign=top><tt>a.size() >= 0 && a.size() <= max_size()</tt></TD></TR><TR><TD VAlign=top>Maximum size</TD><TD VAlign=top><tt>a.max_size()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Returns the largest size that this container can ever have. <A href="#8">[8]</A></TD><TD VAlign=top><tt>a.max_size() >= 0 && a.max_size() >= a.size()</tt></TD></TR><TR><TD VAlign=top>Empty container</TD><TD VAlign=top><tt>a.empty()</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Equivalent to <tt>a.size() == 0</tt>. (But possibly faster.)</TD><TD VAlign=top> </TD></TR><TR><TD VAlign=top>Swap</TD><TD VAlign=top><tt>a.swap(b)</tt></TD><TD VAlign=top> </TD><TD VAlign=top>Equivalent to <tt>swap(a,b)</tt> <A href="#9">[9]</A></TD><TD VAlign=top> </TD></tr></table><h3>Complexity guarantees</h3>The copy constructor, the assignment operator, and the destructor are linear in the container's size.<P><tt>begin()</tt> and <tt>end()</tt> are amortized constant time.<P><tt>size()</tt> is linear in the container's size. <A href="#10">[10]</A> <tt>max_size()</tt> and <tt>empty()</tt> are amortized constant time. If you are testing whether a container is empty, you should always write <tt>c.empty()</tt> instead of <tt>c.size() == 0</tt>. The two expressions are equivalent, but the former may be much faster.<P><tt>swap()</tt> is amortized constant time. <A href="#9">[9]</A><h3>Invariants</h3><Table border><TR><TD VAlign=top>Valid range</TD><TD VAlign=top>For any container <tt>a</tt>, <tt>[a.begin(), a.end())</tt> is a valid range. <A href="#11">[11]</A></TD></TR><TR><TD VAlign=top>Range size</TD><TD VAlign=top><tt>a.size()</tt> is equal to the distance from <tt>a.begin()</tt> to <tt>a.end()</tt>.</TD></TR><TR><TD VAlign=top>Completeness</TD><TD VAlign=top>An algorithm that iterates through the range <tt>[a.begin(), a.end())</tt> will pass through every element of <tt>a</tt>. <A href="#11">[11]</A></TD></tr></table><h3>Models</h3><UL><LI> <A href="Vector.html">vector</A></UL><h3>Notes</h3><P><A name="1">[1]</A>The fact that the lifetime of elements cannot exceed that of oftheir container may seem like a severe restriction. In fact, though,it is not. Note that pointers and iterators are objects; like anyother objects, they may be stored in a container. The container, inthat case, "owns" the pointers themselves, but not the objects thatthey point to.<P><A name="2">[2]</A>This expression must be a <tt>typedef</tt>, that is, a synonym fora type that already has some other name.<P><A name="3">[3]</A>This may either be a <tt>typedef</tt> for some other type, or elsea unique type that is defined as a nested class within the class <tt>X</tt>.<P><A name="4">[4]</A>A container's iterator type and const iterator type may be thesame: there is no guarantee that every container must have anassociated mutable iterator type. For example, <tt><A href="set.html">set</A></tt> and<tt><A href="hash_set.html">hash_set</A></tt> define <tt>iterator</tt> and <tt>const_iterator</tt> to bethe same type.<P><A name="5">[5]</A>It is required that the reference type has the same semantics asan ordinary C++ reference, but it need not actually be an ordinary C++reference. Some implementations, for example, might provideadditional reference types to support non-standard memory models.Note, however, that "smart references" (user-defined reference typesthat provide additional functionality) are not a viable option. It isimpossible for a user-defined type to have the same semantics as C++references, because the C++ language does not support redefining themember access operator (<tt>operator.</tt>).<P><A name="6">[6]</A>As in the case of references <A href="#5">[5]</A>, the pointer type must have thesame semantics as C++ pointers but need not actually be a C++ pointer."Smart pointers," however, unlike "smart references", are possible.This is because it is possible for user-defined types to define thedereference operator and the pointer member access operator, <tt>operator*</tt> and <tt>operator-></tt>.<P><A name="7">[7]</A>The iterator type need only be an <i>input iterator</i>, whichprovides a very weak set of guarantees; in particular, all algorithmson input iterators must be "single pass". It follows that only a singleiterator into a container may be active at any one time. This restrictionis removed in <A href="ForwardContainer.html">Forward Container</A>.<P><A name="8">[8]</A>In the case of a fixed-size container, <tt>size() == max_size()</tt>.<P><A name="9">[9]</A>For any <A href="Assignable.html">Assignable</A> type, <A href="swap.html">swap</A> can be defined in terms ofassignment. This requires three assignments, each of which, for a container type, is linear in the container's size. In a sense,then, <tt>a.swap(b)</tt> is redundant. It exists solely for the sake ofefficiency: for many containers, such as <A href="Vector.html">vector</A> and <A href="List.html">list</A>,it is possible to implement <tt>swap</tt> such that its run-time complexityis constant rather than linear. If this is possible for some containertype <tt>X</tt>, then the template specialization <tt><A href="swap.html">swap</A>(X&, X&)</tt> cansimply be written in terms of <tt>X::swap(X&)</tt>. The implication of thisis that <tt>X::swap(X&)</tt> should <b>only</b> be defined if there exists sucha constant-time implementation. Not every container class <tt>X</tt> needhave such a member function, but if the member function exists at all thenit is guaranteed to be amortized constant time.<P><A name="10">[10]</A>For many containers, such as <tt><A href="Vector.html">vector</A></tt> and <tt><A href="Deque.html">deque</A></tt>, <tt>size</tt> is<i>O(1</i>). This satisfies the requirement that it be <i>O(N</i>).<P><A name="11">[11]</A>Although <tt>[a.begin(), a.end())</tt> must be a valid range, and mustinclude every element in the container, the order in which the elementsappear in that range is unspecified. If you iterate through a containertwice, it is not guaranteed that the order will be the same bothtimes. This restriction is removed in <A href="ForwardContainer.html">Forward Container</A>.<h3>See also</h3>The <A href="Iterators.html">Iterator overview</A>, <A href="InputIterator.html">Input Iterator</A>, <A href="Sequence.html">Sequence</A><!-- start footer --><!-- Footer Begins --><STYLE TYPE="text/css"><!--TD.footer, TD.footer A{ font-family: Arial, helvetica, sans-serif; font-size: 8pt;}A.home {font-family: Arial, helvetica, sans-serif;}--></STYLE><P><A CLASS="home" HREF="index.html">STL Home</A><P><TABLE WIDTH="600" CELLPADDING="0" CELLPADDING="0" BORDER="0"> <TR> <TD ALIGN="RIGHT" CLASS="footer"><A HREF="/company_info/terms.html" TARGET="_top">terms of use</A> | <A HREF="/company_info/privacy.html" TARGET="_top">privacy policy</A></TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/cgi-bin/feedback/" TARGET="_top">contact us</A></TD> </TR><TR> <TD ALIGN="RIGHT" CLASS="footer">Copyright © 1993-2003 Silicon Graphics, Inc. All rights reserved.</TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/company_info/trademarks/" TARGET="_top">Trademark Information</A></TD> </TR></TABLE><!-- Footer Ends --><!-- end footer --><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -