📄 set_1735.htm
字号:
<HTML><TITLE>set_symmetric_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_symmetric_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_symmetric_difference </B>(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
OutputIterator result);
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator
<B>set_symmetric_difference </B>(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
OutputIterator result, Compare comp);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P><B><I>set_symmetric_difference</B></I> constructs a sorted symmetric difference of the elements from the two ranges. This means that the constructed range includes copies of the elements that are present in the range <SAMP>[first1, last1)</SAMP> but not present in the range <SAMP>[first2, last2)</SAMP><I>and</I> copies of the elements that are present in the range <SAMP>[first2, last2)</SAMP> but not in the range <SAMP>[first1, last1)</SAMP>. It returns the end of the constructed range. </P>
<P>For example, suppose we have 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 <B><I>set_symmetric_difference</B></I> of these two sets is:</P>
<PRE>1 2 6 7</PRE>
<PRE></PRE><P>The result of <B><I>set_symmetric_difference</B></I> is undefined if the result range overlaps with either of the original ranges. </P>
<P><B><I>set_symmetric_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_symmetric_difference</B></I> algorithm to return a result that includes elements that are present in the first set and not in the second.</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_s_di.cpp
//
#include<algorithm>
#include<set>
#include <istream.h>
int main()
{
//Initialize some sets
int a1[] = {1,3,5,7,9,11};
int a3[] = {3,5,7,8};
set<int, less<int> > odd(a1,a1+6), result,
small(a3,a3+4);
//Create an insert_iterator for result
insert_iterator<set<int, less<int> > >
res_ins(result, result.begin());
//Demonstrate set_symmetric_difference
cout << "The symmetric difference of:" << endl << "{";
copy(small.begin(),small.end(),
ostream_iterator<int>(cout," "));
cout << "} with {";
copy(odd.begin(),odd.end(),
ostream_iterator<int>(cout," "));
cout << "} =" << endl << "{";
<B>set_symmetric_difference</B>(small.begin(), small.end(),
odd.begin(), odd.end(), res_ins);
copy(result.begin(),result.end(),
ostream_iterator<int>(cout," "));
cout << "}" << endl << endl;
return 0;
}
Output :
The symmetric difference of:
{3 5 7 8 } with {1 3 5 7 9 11 } =
{1 8 9 11 }</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_0972.htm"><B><I>set_difference</B></I></A></P>
<HR>
<A HREF="set_9182.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="set_6462.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -