Hi,
I have some questions regarding STL set intersection. As per the STL
reference manual (http://www.hpl.hp.com/techreports/95/HPL-95-11.pdf),
the signatures of the set_intersection operation are as follows:
template <class InputIterator1, class InputIterator2, class
OutputIterator>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1
last1,
InputIterator2 first2, InputIterator2 last2, OutputIterator result);
template <class InputIterator1, class InputIterator2, class
OutputIterator,
class Compare>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1
last1,
InputIterator2 first2, InputIterator2 last2, OutputIterator result,
Compare comp);
I am looking at an example of set_intersection code in The C++ Standard
Library - A Tutorial and Reference by Nicolai M. Josuttis
(http://www.josuttis.com/libbook/). I have taken the liberty of
reproducing the sample code below.
int main()
{
int c1[] = { 1, 2, 2, 4, 6, 7, 7, 9 };
int num1 = sizeof(c1) / sizeof(int);
int c2[] = { 2, 2, 2, 3, 6, 6, 8, 9 };
int num2 = sizeof(c2) / sizeof(int);
// <Snip>
// intersect the ranges by using set_intersection()
cout << "set_intersection(): ";
set_intersection (c1, c1+num1,
c2, c2+num2,
ostream_iterator<int>(cout," "));
cout << endl;
// <Snip>
}
Two questions
===========
(1) I was expecting that when set_intersection is invoked, we would
have to pass the data-type information SINCE set_intersection IS
DEFINED AS A TEMPLATE. How can we invoke set_intersection without
passing the data types?
(2) I am having trouble declaring the OutputIterator to hold the result
(instead of printing the contents of the intersection, as in the sample
code). Basically I have tried a few things (including back_inserter,
front_inserter and inserter ----- I got that idea from STL Tutorial and
Reference Guide: C++ Programming with the Standard Template Library by
David R. Musser, Gillmer J. Derge & Atul Saini) but I keep getting
different compiler errors. Can someone post the correct syntax for
that please?
Let me repeat what I want. I want to be able to iterate through the
intersection once I return from the set_intersection call.
Thanks in advance,
Gus