473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

STL:: Questions on set_intersection

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
3 6063
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
25
by: CodeCracker | last post by:
Problem details below: I have few items(simple values or objects) to be put into an array and I implement it through a set rather than just an array of items. This is because every time I get a...
7
by: Davy | last post by:
Hi all, I want to use set_intersection, and the compiler passed with a warning. But the program hang on when it enter set_intersection(), why? And how can I insert element to set by insert();...
2
by: harris.pc | last post by:
Hi, Can someone please explain to me WHY the standard does not allow the input range and the output range to overlap for set_intersection? I can't see why I can't do this (note sorted vectors,...
8
by: Generic Usenet Account | last post by:
I have a need for a set operation (actually multi-set operation) on sorted structures that is not in the STL library. I call it the set_retain operation. It's kinda similar to the...
4
by: Yuri CHUANG | last post by:
This is a example from a textbook,but there are some strange error that I don't understand.Could anyone give me some help to realize the operations on set.Thank you very much:-) (I compile it with...
1
by: Phil | last post by:
Hello, I'm trying to get the result of a set intersection and I'm sure that I've made a very basic error. A Google search has helped but I'm still not quite there. Can someone show me the...
1
by: shark | last post by:
Hi, all. This time I met a problem in "inserter" functional. My program is like the following: /////////////////////////////////////////////////////////////////////////// #include <iostream>...
2
by: keindl | last post by:
I'd like to test if two sorted vectors intersect each other. What is the most efficient implementation for this? I could calculate the intersection but that would not be efficient since after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.