📄 set_0972.htm
字号:
<HTML><TITLE>set_difference</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>set_difference</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Basic set operation for sorted sequences.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class InputIterator1, class InputIterator2,
class OutputIterator>
OutputIterator
<B>set_difference</B> (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator
<B>set_difference </B>(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>set_difference</B></I> algorithm constructs a sorted difference that includes copies of the elements that are present in the range <SAMP>[first1, last1)</SAMP> but are not present in the range <SAMP>[first2, last2)</SAMP>. It returns the end of the constructed range. </P>
<P>As an example, assume we have the following two sets:</P>
<PRE>1 2 3 4 5</PRE>
<PRE></PRE><P>and </P>
<PRE>3 4 5 6 7</PRE>
<PRE></PRE><P>The result of applying <B><I>set_difference</B></I> is the set:</P>
<PRE>1 2</PRE>
<PRE></PRE><P>The result of <B><I>set_difference</B></I> is undefined if the result range overlaps with either of the original ranges. </P>
<P><B><I>set_difference</B></I> assumes that the ranges are sorted using the default comparision operator less than (<SAMP><</SAMP>), unless an alternative comparison operator (<SAMP>comp</SAMP>) is provided. </P>
<P>Use the <B><I>set_symetric_difference</B></I> algorithm to return a result that contains all elements that are not in common between the two sets.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>At most <SAMP>((last1 - first1) + (last2 - first2)) * 2 -1</SAMP> comparisons are performed.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// set_diff.cpp
//
#include <algorithm>
#include <set>
#include <iostream.h></PRE><PRE> int main()
{</PRE>
<PRE> //Initialize some sets
int a1[10] = {1,2,3,4,5,6,7,8,9,10};
int a2[6] = {2,4,6,8,10,12};</PRE><PRE> set<int, less<int> > all(a1, a1+10), even(a2, a2+6),
odd;</PRE>
<PRE> //Create an insert_iterator for odd
insert_iterator<set<int, less<int> > >
odd_ins(odd, odd.begin());
//Demonstrate set_difference
cout << "The result of:" << endl << "{";
copy(all.begin(),all.end(),
ostream_iterator<int>(cout," "));
cout << "} - {";
copy(even.begin(),even.end(),
ostream_iterator<int>(cout," "));
cout << "} =" << endl << "{";
<B>set_difference</B>(all.begin(), all.end(),
even.begin(), even.end(), odd_ins);
copy(odd.begin(),odd.end(),
ostream_iterator<int>(cout," "));
cout << "}" << endl << endl;</PRE><PRE> return 0;
}</PRE>
<PRE>Output :
The result of:
{1 2 3 4 5 6 7 8 9 10 } - {2 4 6 8 10 12 } =
{1 3 5 7 9 }
</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you need to always supply the <SAMP>Compare</SAMP> template argument and the <SAMP>Allocator</SAMP> template argument. For instance, you will 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="inc_3676.htm"><B><I>includes</B></I></A>, <A HREF="set_1649.htm"><B><I>set</B></I></A>, <A HREF="set_6462.htm"><B><I>set_union</B></I></A>,<A HREF="set_9182.htm"><B><I> set_intersection</B></I></A>, <A HREF="set_1735.htm"><B><I>set_symmetric_difference</B></I></A></P>
<HR>
<A HREF="set_1649.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="set_9182.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -