By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

STL:: Questions on set_intersection

P: n/a
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

Oct 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Generic Usenet Account" <us****@sta.samsung.com> wrote:
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::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// <Snip>
}

-Howard
Oct 11 '05 #2

P: n/a
Howard Hinnant wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Generic Usenet Account" <us****@sta.samsung.com> wrote:
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 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

Oct 11 '05 #3

P: n/a
On 11 Oct 2005 15:16:30 -0700, "Generic Usenet Account"
<us****@sta.samsung.com> wrote:
Howard Hinnant wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Generic Usenet Account" <us****@sta.samsung.com> wrote:
> 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 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::ostream_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
Oct 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.