Connecting Tech Pros Worldwide Help | Site Map

STL:: Questions on set_intersection

  #1  
Old October 11th, 2005, 12:15 AM
Generic Usenet Account
Guest
 
Posts: 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

  #2  
Old October 11th, 2005, 06:45 PM
Howard Hinnant
Guest
 
Posts: n/a

re: STL:: Questions on set_intersection


In article <1128985339.876336.140970@g14g2000cwa.googlegroups .com>,
"Generic Usenet Account" <usenet@sta.samsung.com> wrote:
[color=blue]
> Let me repeat what I want. I want to be able to iterate through the
> intersection once I return from the set_intersection call.[/color]

#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
  #3  
Old October 11th, 2005, 11:25 PM
Generic Usenet Account
Guest
 
Posts: n/a

re: STL:: Questions on set_intersection


Howard Hinnant wrote:[color=blue]
> In article <1128985339.876336.140970@g14g2000cwa.googlegroups .com>,
> "Generic Usenet Account" <usenet@sta.samsung.com> wrote:
>[color=green]
> > Let me repeat what I want. I want to be able to iterate through the
> > intersection once I return from the set_intersection call.[/color]
>[/color]

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

  #4  
Old October 12th, 2005, 12:25 AM
Andy Buchanan
Guest
 
Posts: n/a

re: STL:: Questions on set_intersection


On 11 Oct 2005 15:16:30 -0700, "Generic Usenet Account"
<usenet@sta.samsung.com> wrote:
[color=blue]
>Howard Hinnant wrote:[color=green]
>> In article <1128985339.876336.140970@g14g2000cwa.googlegroups .com>,
>> "Generic Usenet Account" <usenet@sta.samsung.com> wrote:
>>[color=darkred]
>> > Let me repeat what I want. I want to be able to iterate through the
>> > intersection once I return from the set_intersection call.[/color]
>>[/color]
>
>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.[/color]

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

[color=blue]
>Also, I would appreciate a response for Query #1.[/color]

|(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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alternatives to the C++ Standard Library? Steven T. Hatton answers 43 July 23rd, 2005 06:52 AM