📄 set_1754.htm
字号:
set_three.erase(five);
// erase all values between seven and eleven
set<int>::iterator seven = set_three.find(7);
set<int>::iterator eleven = set_three.find(11);
set_three.erase (seven, eleven);
</PRE>
<P>If the underlying element type provides a destructor, then the destructor will be invoked prior to removing the element from the collection.</P>
<A NAME="searchingandcounting"><H3>Searching and Counting</H3></A>
<P>The member function <SAMP>size()</SAMP> will yield the number of elements held by a container. The member function <SAMP>empty()</SAMP> will return a boolean true value if the container is empty, and is generally faster than testing the size against zero.</P>
<P>The member function <SAMP>find()</SAMP> takes an element value, and returns an iterator denoting the location of the value in the set if it is present, or a value matching the end-of-set (the value yielded by the function <SAMP>end()</SAMP>) if it is not. If a multiset contains more than one matching element, the value returned can be any appropriate value.</P>
<PRE> set<int>::iterator five = set_three.find(5);
if (five != set_three.end())
cout << "set contains a five" << endl;
</PRE>
<P>The member functions<SAMP> lower_bound()</SAMP> and <SAMP>upper_bound()</SAMP> are most useful with multisets, as with sets they simply mimic the function <SAMP>find().</SAMP> The member function<SAMP> lower_bound()</SAMP> yields the first entry that matches the argument key, while the member function <SAMP>upper_bound()</SAMP> returns the first value past the last entry matching the argument. Finally, the member function <SAMP>equal_range()</SAMP> returns a <A HREF="../stdref/pai_5818.htm"><B><I>pair</I></B></A> of iterators, holding the lower and upper bounds.</P>
<P>The member function<SAMP> count()</SAMP> returns the number of elements that match the argument. For a set this value is either zero or one, whereas for a multiset it can be any nonnegative value. Since a non-zero integer value is treated as true, the <SAMP>count()</SAMP> function can be used to test for inclusion of an element, if all that is desired is to determine whether or not the element is present in the set. The alternative, using <SAMP>find(),</SAMP> requires testing the result returned by <SAMP>find()</SAMP> against the end-of-collection iterator.</P>
<PRE> if (set_three.count(5))
cout << "set contains a five" << endl;
</PRE>
<A NAME="iterators"><H3>Iterators</H3></A>
<A HREF="sidebar.htm#sidebar27"><IMG SRC="images/note.gif" BORDER=0> <STRONG>No Iterator Invalidation</STRONG></A>
<P>The member functions <SAMP>begin()</SAMP> and <SAMP>end()</SAMP> produce iterators for both sets and multisets. The iterators produced by these functions are constant to ensure that the ordering relation for the set is not inadvertently or intentionally destroyed by assigning a new value to a set element. Elements are generated by the iterators in sequence, ordered by the comparison operator provided when the set was declared. The member functions <SAMP>rbegin()</SAMP> and <SAMP>rend()</SAMP> produce iterators that yield the elements in reverse order. </P>
<A NAME="setoperations"><H3>Set Operations</H3></A>
<P>The traditional set operations of subset <SAMP>test</SAMP>, set <SAMP>union</SAMP>, set <SAMP>intersection</SAMP>, and set <SAMP>difference</SAMP> are not provided as member functions, but are instead implemented as generic algorithms that will work with any ordered structure. These functions are described in more detail in <a href="set_1753.htm">Chapter 14 (<i>Set Operations</i>)</a>. The following summary describes how these functions can be used with the set and multiset container classes.</P>
<H4>Subset test</H4>
<P>The function <SAMP>includes()</SAMP> can be used to determine if one set is a subset of another; that is, if all elements from the first are contained in the second. In the case of multisets the number of matching elements in the second set must exceed the number of elements in the first. The four arguments are a pair of iterators representing the (presumably) smaller set, and a pair of iterators representing the (potentially) larger set.</P>
<PRE> if (includes(set_one.begin(), set_one.end(),
set_two.begin(), set_two.end()))
cout << "set_one is a subset of set_two" << endl;
</PRE>
<P>The less than operator (operator <SAMP><</SAMP>) will be used for the comparison of elements, regardless of the operator used in the declaration of the set. Where this is inappropriate, an alternative version of the <SAMP>includes()</SAMP> function is provided. This form takes a fifth argument, which is the comparison function used to order the elements in the two sets.</P>
<H4>Set Union or Intersection</H4>
<P>The function <SAMP>set_union()</SAMP> can be used to construct a union of two sets. The two sets are specified by iterator pairs, and the union is copied into an output iterator that is supplied as a fifth argument. To form the result as a set, an <I>insert iterator</I> must be used to form the output iterator. (<i>See <a href="ins_0332.htm">Chapter 2: <i>Insert Iterators</i></a> for a discussion of insert iterators</i>.) If the desired outcome is a union of one set with another, then a temporary set can be constructed, and the results swapped with the argument set prior to deletion of the temporary set.</P>
<PRE> // union two sets, copying result into a vector
vector<int> v_one (set_one.size() + set_two.size());
set_union(set_one.begin(), set_one.end(),
set_two.begin(), set_two.end(), v_one.begin());
// form union in place
set<int> temp_set;
set_union(set_one.begin(), set_one.end(),
set_two.begin(), set_two.end(),
inserter(temp_set, temp_set.begin()));
set_one.swap(temp_set); // temp_set will be deleted
</PRE>
<P>The function <SAMP>set_intersection()</SAMP> is similar, and forms the intersection of the two sets.</P>
<P>As with the <SAMP>includes()</SAMP> function, the less than operator (operator <SAMP><</SAMP>) is used to compare elements in the two argument sets, regardless of the operator provided in the declaration of the sets. Should this be inappropriate, alternative versions of both the <SAMP>set_union()</SAMP> or <SAMP>set_intersection()</SAMP> functions permit the comparison operator used to form the set to be given as a sixth argument.</P>
<P>The operation of taking the union of two multisets should be distinguished from the operation of merging two sets. Imagine that one argument set contains three instances of the element 7, and the second set contains two instances of the same value. The union will contain only three such values, while the merge will contain five. To form the merge, the function <SAMP>merge()</SAMP> can be used (<i>see <a href="mer_3553.htm">Chapter 14: Merge Ordered Sequences</a></i>). The arguments to this function exactly match those of the <SAMP>set_union()</SAMP> function.</P>
<H4>Set Difference</H4>
<P>There are two forms of set difference. A simple set difference represents the elements in the first set that are not contained in the second. A symmetric set difference is the union of the elements in the first set that are not contained in the second, with the elements in the second that are not contained in the first. These two values are constructed by the functions <SAMP>set_difference()</SAMP> and <SAMP>set_symmetric_difference(),</SAMP> respectively. The use of these functions is similar to the use of the <SAMP>set_union()</SAMP> function described earlier.</P>
<A NAME="othergenericalgorithms"><H3>Other Generic Algorithms</H3></A>
<P>Because sets are ordered and have constant iterators, a number of the generic functions described in Chapters <a href="gen_9895.htm">13</a> and <a href="ord_1635.htm">14</a> either are not applicable to sets or are not particularly useful. However, the following table gives a few of the functions that can be used in conjunction with the <A HREF="../stdref/set_1649.htm"><B><I>set</I></B></A> data type.</P>
<CENTER><TABLE BORDER CELLSPACING=3 CELLPADDING=3>
<TR VALIGN=top>
<TD>
<B><I>Purpose</I></B><BR>
</TD>
<TD>
<B><I>Name</I></B><BR>
</TD>
<TD>
<B><I>Chapter / Section</I></B><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Copy one sequence into another <BR>
</TD>
<TD>
<SAMP>copy</SAMP><BR>
</TD>
<TD>
<a href="ini_5794.htm#copyonesequenceintoanothersequence">13 (<i>Copy One Sequence into Another Sequence</i>)</a><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Find an element that matches a condition <BR>
</TD>
<TD>
<SAMP>find_if</SAMP><BR>
</TD>
<TD>
<a href="sea_9743.htm#findanelementsatisfyingacondition">13 (<i>Find an Element Satisfying a Condition</i>)</a><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Find a subsequence within a set <BR>
</TD>
<TD>
<SAMP>search</SAMP><BR>
</TD>
<TD>
<a href="sea_9743.htm#findasubsequencewithinasequence">13 (<i>Find a Subsequence within a Sequence</i>)</a><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Count number of elements that satisfy condition <BR>
</TD>
<TD>
<SAMP>count_if</SAMP><BR>
</TD>
<TD>
<a href="sca_1926.htm#countthenumberofelementsthatsatisfyacondition">13 (<i>Count the Number of Elements...</i>)</a><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Reduce set to a single value <BR>
</TD>
<TD>
<SAMP>accumulate</SAMP><BR>
</TD>
<TD>
<a href="sca_1926.htm#reducesequencetoasinglevalue">13 (<i>Reduce Sequence to a Single Value</i>)</a><BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
Execute function on each element <BR>
</TD>
<TD>
<SAMP>for_each</SAMP><BR>
</TD>
<TD>
<a href="mis_2456.htm#applyafunctiontoallelementsinacollection">13 (<i>Apply a Function to All Elements in a Collection</i>)</a><BR>
</TD>
</TR>
</TABLE></CENTER>
<HR>
<A HREF="set_4687.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc.htm"><IMG SRC="images/toc.gif"></A> <A HREF="exa_2257.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -