STL:: Questions on set_intersection

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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
 
H

Howard Hinnant

"Generic Usenet Account said:
Let me repeat what I want. I want to be able to iterate through the
intersection once I return from the set_intersection call.

#include <algorithm>
#include <iostream>

int main()
{
int c1[] = { 1, 2, 2, 4, 6, 7, 7, 9 };
const int num1 = sizeof(c1) / sizeof(int);

int c2[] = { 2, 2, 2, 3, 6, 6, 8, 9 };
const int num2 = sizeof(c2) / sizeof(int);

int c3[num1];

// <Snip>

// intersect the ranges by using set_intersection()
std::cout << "set_intersection(): ";
int* c3end = std::set_intersection (c1, c1+num1,
c2, c2+num2,
c3);
copy(c3, c3end, std::eek:stream_iterator<int>(std::cout, " "));
std::cout << '\n';
// <Snip>
}

-Howard
 
G

Generic Usenet Account

Thanks for the coding example. Actually I wanted a coding example
where the two sets in question are STL sets (or multisets) and not
arrays. My apologies for not mentioning this explicitly.


Also, I would appreciate a response for Query #1.

Thanks,
Gus
 
A

Andy Buchanan

Thanks for the coding example. Actually I wanted a coding example
where the two sets in question are STL sets (or multisets) and not
arrays. My apologies for not mentioning this explicitly.

How about this?

#include <algorithm>
#include <set>
#include <iostream>

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);

std::multiset<int> s1( c1, c1+num1 );
std::multiset<int> s2( c2, c2+num2 );
std::multiset<int> s3;

// intersect the ranges by using set_intersection()
std::cout << "set_intersection(): ";
std::set_intersection(
s1.begin(), s1.end(),
s2.begin(), s2.end(),
std::inserter( s3, s3.begin() )
);
std::copy(
s3.begin(), s3.end(),
std::eek:stream_iterator<int>( std::cout, " " )
);
std::cout << '\n';

return 0;
}

Also, I would appreciate a response for Query #1.

|(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?

The iterators know the type(s).



R.
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top