📄 set_1754.htm
字号:
<HTML><HEAD><TITLE>set and multiset Operations</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>set and multiset Operations</H2>
<A HREF="sidebar.htm#sidebar24"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Sets and Bags</STRONG></A>
<P>The member functions provided by the set and multiset data types will shortly be described in more detail. Note that while member functions provide basic operations, the utility of these data structures is greatly extended through the use of the generic algorithms described in Chapters <a href="gen_9895.htm">13</a> and <a href="ord_1635.htm">14</a>.</P>
<A NAME="declarationandinitializationofset"><H3>Declaration and Initialization of Set</H3></A>
<P>A <A HREF="../stdref/set_1649.htm"><B><I>set</I></B></A> is a template data structure, specialized by the type of the elements it contains, and the operator used to compare keys. The latter argument is optional, and, if it is not provided, the less than operator for the key type will be assumed. The element type can be a primitive language type (such as integer or double), a pointer type, or a user-defined type. The element type must recognize both the equality testing operator (operator <SAMP>==</SAMP>) and the less than comparison operator (operator <SAMP><</SAMP>).</P>
<A HREF="sidebar.htm#sidebar25"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Initializing Sets with Iterators</STRONG></A>
<P>Sets can be declared with no initial elements, or they can be initialized from another container by providing a pair of iterators. An optional argument in both cases is an alternative comparison function; this value overrides the value provided by the template parameter. This mechanism is useful if a program contains two or more sets with the same values but different orderings, as it prevents more than one copy of the set member function from being instantiated. The copy constructor can be used to form a new set that is a clone, or copy, of an existing set.</P>
<PRE> set <int> set_one;
set <int, greater<int> > set_two;
set <int> set_three(greater<int>());
set <gadget, less<gadget> > gset;
set <gadget> gset(less<gadget>());
set <int> set_four (aList.begin(), aList.end());
set <int> set_five
(aList.begin(), aList.end(), greater<int>());
set <int> set_six (set_four); // copy constructor
</PRE>
<P>A set can be assigned to another set, and two sets can exchange their values using the <SAMP>swap()</SAMP> operation (in a manner analogous to other standard library containers).</P>
<PRE> set_one = set_five;
set_six.swap(set_two);
</PRE>
<A NAME="typedefinitions"><H3>Type Definitions</H3></A>
<P>The classes <A HREF="../stdref/set_1649.htm"><B><I>set</I></B></A> and <A HREF="../stdref/mul_0958.htm"><B><I>multiset</I></B></A> include a number of type definitions. The most common use for these is in a declaration statement. For example, an iterator for a set of integers can be declared in the following fashion:</P>
<PRE> set<int>::iterator location;
</PRE>
<P>In addition to <SAMP>iterator</SAMP>, the following types are defined:</P>
<CENTER><TABLE CELLSPACING=3 CELLPADDING=3>
<TR VALIGN=top>
<TD>
<SAMP>value_type</SAMP><BR>
</TD>
<TD>
The type associated with the elements the set maintains.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_iterator</SAMP><BR>
</TD>
<TD>
An iterator that does not allow modification of the underlying sequence.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>reverse_iterator</SAMP><BR>
</TD>
<TD>
An iterator that moves in a backward direction.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_reverse_iterator</SAMP><BR>
</TD>
<TD>
A combination constant and reverse iterator.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>reference</SAMP><BR>
</TD>
<TD>
A reference to an underlying element.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_reference</SAMP><BR>
</TD>
<TD>
A reference to an underlying element that will not permit modification.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>size_type</SAMP><BR>
</TD>
<TD>
An unsigned integer type, used to refer to the size of containers.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>value_compare</SAMP><BR>
</TD>
<TD>
A function that can be used to compare two elements.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>difference_type</SAMP><BR>
</TD>
<TD>
A signed integer type, used to describe the distance between iterators.<BR>
</TD>
</TR>
</TABLE></CENTER>
<A NAME="insertion"><H3>Insertion</H3></A>
<A HREF="sidebar.htm#sidebar26"><IMG SRC="images/note.gif" BORDER=0> <STRONG>The Pair Data Type</STRONG></A>
<P>Unlike a list or vector, there is only one way to add a new element to a <A HREF="../stdref/set_1649.htm"><B><I>set</I></B></A>. A value can be inserted into a set or a multiset using the <SAMP>insert()</SAMP> member function. With a multiset, the function returns an iterator that denotes the value just inserted. Insert operations into a <B><I>set</I></B> return a <A HREF="../stdref/pai_5818.htm"><B><I>pair</I></B></A> of values, in which the first field contains an iterator, and the second field contains a boolean value that is true if the element was inserted, and false otherwise. Recall that in a set, an element will not be inserted if it matches an element already contained in the collection.</P>
<PRE> set_one.insert (18);
if (set_one.insert(18).second)
cout << "element was inserted" << endl;
else
cout << "element was not inserted " << endl;
</PRE>
<P>Insertions of several elements from another container can also be performed using an iterator pair:</P>
<PRE> set_one.insert (set_three.begin(), set_three.end());
</PRE>
<P>The <A HREF="../stdref/pai_5818.htm"><B><I>pair</I></B></A> data structure is a tuple of values. The first value is accessed through the field name <SAMP>first</SAMP>, while the second is, naturally, named <SAMP>second</SAMP>. A function named <SAMP>make_pair()</SAMP> simplifies the task of producing an instance of class <B><I>pair</I></B>.</P>
<PRE>template <class T1, class T2>
struct pair {
T1 first;
T2 second;
pair (const T1 & x, const T2 & y) : first(x), second(y) { }
};
template <class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y)
{ return pair<T1, T2>(x, y); }
</PRE>
<P>In determining the equivalence of keys, for example, to determine if the key portion of a new element matches any existing key, the comparison function for keys is used, and not the equivalence (<SAMP>==</SAMP>) operator. Two keys are deemed equivalent if the comparison function used to order key values yields false in both directions. That is, if <SAMP>Compare(key1, key2)</SAMP> is false, and if <SAMP>Compare(key2, key1)</SAMP> is false, then <SAMP>key1</SAMP> and <SAMP>key2</SAMP> are considered equivalent.</P>
<A NAME="removalofelementsfromaset"><H3>Removal of Elements from a Set</H3></A>
<P>Values are removed from a set using the member function <SAMP>erase().</SAMP> The argument can be either a specific value, an iterator that denotes a single value, or a pair of iterators that denote a range of values. When the first form is used on a multiset, all arguments matching the argument value are removed, and the return value indicates the number of elements that have been erased.</P>
<PRE> // erase element equal to 4
set_three.erase(4);
// erase element five
set<int>::iterator five = set_three.find(5);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -