📄 set_1649.htm
字号:
<B>clear</B> ();</PRE>
<UL><P>Erases all elements from the set.</P>
</UL>
<PRE>size_type
<B>count</B> (const key_type& x) const;</PRE>
<UL><P>Returns the number of elements equal to <SAMP>x</SAMP>. Since a set supports unique keys, <SAMP>count</SAMP> will always return 1 or 0.</P>
</UL>
<PRE>bool
<B>empty</B> () const;</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the size is zero.</P>
</UL>
<PRE>pair<iterator, iterator>
<B>equal_range</B> (const key_type& x) const;</PRE>
<UL><P>Returns <SAMP>pair(lower_bound(x),upper_bound(x))</SAMP>. The <SAMP>equal_range</SAMP> function indicates the valid range for insertion of <SAMP>x</SAMP> into the set.</P>
</UL>
<PRE>size_type
<B>erase</B> (const key_type& x);</PRE>
<UL><P>Deletes all the elements matching <SAMP>x</SAMP>. Returns the number of elements erased. Since a set supports unique keys, <SAMP>erase</SAMP> will always return 1 or 0.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator position);</PRE>
<UL><P>Deletes the map 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 item was the last one in this list.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator first, iterator last);</PRE>
<UL><P>Deletes the elements in the range (<SAMP>first, last</SAMP>). Returns an <SAMP>iterator</SAMP> pointing to the element following the last deleted element, or <SAMP>end()</SAMP> if there were no elements after the deleted range.</P>
</UL>
<PRE>iterator
<B>find</B> (const key_value& x) const;</PRE>
<UL><P>Returns an <SAMP>iterator</SAMP> that points to the element equal to <SAMP>x</SAMP>. If there is no such element, the iterator points to the past-the-end value.</P>
</UL>
<PRE>pair<iterator, bool>
<B>insert</B> (const value_type& x);</PRE>
<UL><P>Inserts <SAMP>x</SAMP> into self according to the comparison function object. The template's default comparison function object is <SAMP>less (<)</SAMP>. If the insertion succeeds, it returns a pair composed of the iterator position where the insertion took place, and <SAMP>true</SAMP>. Otherwise, the pair contains the end value, and <SAMP>false</SAMP>.</P>
</UL>
<PRE>iterator
<B>insert</B> (iterator position, const value_type& x);</PRE>
<UL><P><SAMP>x</SAMP> is inserted into the set. A position may be supplied as a hint regarding where to do the insertion. If the insertion may be done right after position then it takes amortized constant time. Otherwise it will take <SAMP>0 (log N)</SAMP> time. The return value points to the inserted <SAMP>x</SAMP>.</P>
</UL>
<PRE>template <class InputIterator>
void
<B>insert</B>(InputIterator first, InputIterator last);</PRE>
<UL><P>Inserts copies of the elements in the range <SAMP>[first, last]</SAMP>.</P>
</UL>
<PRE>key_compare
<B>key_comp </B>() const;</PRE>
<UL><P>Returns the comparison function object for the set.</P>
</UL>
<PRE>iterator
<B>lower_bound</B> (const key_type& x) const;</PRE>
<UL><P>Returns an <SAMP>iterator</SAMP> that points to the first element that is greater than or equal to <SAMP>x</SAMP>. If there is no such element, the iterator points to the past-the-end value.</P>
</UL>
<PRE>size_type
<B>max_size</B> () const;</PRE>
<UL><P>Returns size<SAMP> </SAMP>of the largest possible set.</P>
</UL>
<PRE>size_type
<B>size</B> () const;</PRE>
<UL><P>Returns the number of elements.</P>
</UL>
<PRE>void
<B>swap</B> (set<Key, Compare, Allocator>& x);</PRE>
<UL><P>Exchanges self with <SAMP>x</SAMP>.</P>
</UL>
<PRE>iterator
<B>upper_bound</B> (const key_type& x) const</PRE>
<UL><P>Returns an iterator that points to the first element that is greater than or equal to <SAMP>x</SAMP>. If there is no such element, the iterator points to the past-the-end value.</P>
</UL>
<PRE>value_compare
<B>value_comp</B> () const;</PRE>
<UL><P>Returns the set's comparison object. This is identical to the function <SAMP>key_comp()</SAMP>.</P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>template <class Key, class Compare, class Allocator>
bool <B>operator== </B>(const set<Key, Compare, Allocator>& x,
const set<Key, Compare, Allocator>& y);</PRE>
<UL><P>Equality operator. Returns <SAMP>true</SAMP> if <SAMP>x</SAMP> is the same as <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class Key, class Compare, class Allocator>
bool <B>operator< </B>(const set <Key, Compare, Allocator>& x,
const set <Key, Compare, Allocator>& y);</PRE>
<UL><P>Returns true if the elements contained in <SAMP>x</SAMP> are lexicographically less than the elements contained in <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class Key, class Compare, class Allocator>
void <B>swap</B> (set <Key, Compare, Allocator>& a,
set <Key, Compare, 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>//
// set.cpp
//
#include <set>
#include <iostream.h>
typedef <B>set</B><double, less<double>, allocator> set_type;
ostream& operator<<(ostream& out, const set_type& s)
{
copy(s.begin(), s.end(),
ostream_iterator<set_type::value_type>(cout," "));
return out;
}
int main(void)
{
// create a set of doubles
set_type sd;
int i;
for(i = 0; i < 10; ++i) {
// insert values
sd.insert(i);
}
// print out the set
cout << sd << endl << endl;
// now let's erase half of the elements in the set
int half = sd.size() >> 1;
set_type::iterator sdi = sd.begin();
advance(sdi,half);
sd.erase(sd.begin(),sdi);
// print it out again
cout << sd << endl << endl;
// Make another set and an empty result set
set_type sd2, sdResult;
for (i = 1; i < 9; i++)
sd2.insert(i+5);
cout << sd2 << endl;
// Try a couple of set algorithms
set_union(sd.begin(),sd.end(),sd2.begin(),sd2.end(),
inserter(sdResult,sdResult.begin()));
cout << "Union:" << endl << sdResult << endl;
sdResult.erase(sdResult.begin(),sdResult.end());
set_intersection(sd.begin(),sd.end(),
sd2.begin(),sd2.end(),
inserter(sdResult,sdResult.begin()));
cout << "Intersection:" << endl << sdResult << endl;
return 0;
}
Output :
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
6 7 8 9 10 11 12 13
Union:
5 6 7 8 9 10 11 12 13
Intersection:
6 7 8 9
</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 set <SAMP><Key, Compare, Allocator></SAMP> that takes two templated iterators:</P>
<PRE>template <class InputIterator>
set (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
</PRE><P><B><I>set</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 set in the following two ways:</P>
<PRE>int intarray[10];
set<int, less<int>, allocator> first_set(intarray, intarray + 10);
set<int, less<int>, allocator> second_set(first_set.begin(),
first_set.end());
</PRE><P>but not this way:</P>
<PRE>set<long, less<long>, allocator> long_set(first_set.begin(),
first_set.end());
</PRE><P>since the <SAMP>long_set</SAMP> and <SAMP>first_set</SAMP> are not the same type.</P>
<P>Also, many compilers do not support default template arguments. If your compiler is one of these you need to always supply the <SAMP>Compare</SAMP> template argument, and the <SAMP>Allocator</SAMP> template argument. For instance, you need to write :</P>
<P><SAMP>set<int, less<int>, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>set<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="Bid_7861.htm"><B><I>Bidirectional Iterators</B></I></A>, <A HREF="Con_2487.htm"><B><I>Containers</B></I></A>, <A HREF="lex_3806.htm"><B><I>lexicographical_compare</B></I></A></P>
<HR>
<A HREF="Seq_6405.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="set_0972.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -