📄 set_9182.htm
字号:
<HTML><TITLE>set_intersection</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_intersection</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_intersection</B> (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator last2,
OutputIterator result);
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator
<B>set_intersection</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_intersection</B></I> algorithm constructs a sorted intersection of elements from the two ranges. It returns the end of the constructed range. When it finds an element present in both ranges, <B><I>set_intersection</B></I> always copies the element from the first range into <SAMP>result</SAMP>. This means that the result of <B><I>set_intersection</B></I> is guaranteed to be stable. The result of <B><I>set_intersection</B></I> is undefined if the result range overlaps with either of the original ranges. </P>
<P><B><I>set_intersection</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>
<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_intr.cpp
//
#include <algorithm>
#include <set>
#include <iostream.h>
int main()
{
//Initialize some sets
int a1[10] = {1,3,5,7,9,11};
int a3[4] = {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_intersection
cout << "The result of:" << endl << "{";
copy(small.begin(),small.end(),
ostream_iterator<int>(cout," "));
cout << "} intersection {";
copy(odd.begin(),odd.end(),
ostream_iterator<int>(cout," "));
cout << "} =" << endl << "{";
<B>set_intersection</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 result of:
{3 5 7 8 } intersection {1 3 5 7 9 11 } =
{3 5 7 }</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_0972.htm"><B><I>set_difference</B></I></A>, <A HREF="set_1735.htm"><B><I>set_symmetric_difference</B></I></A></P>
<HR>
<A HREF="set_0972.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="set_1735.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -