473,406 Members | 2,345 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,406 software developers and data experts.

[STL] How to use set_intersection

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();
BTW, I use VC6.0(is it too old?).

//---The code listed:---//
#include <math.h>
#include <fstream.h>
#include <algorithm>
#include <iterator>
#include <set>

using namespace std;
typedef set<int> Set_int;
typedef set<int>:: iterator It;

int main() {
int int_1[]={1,4,3,2,5};
int int_2[]={7,8,4,5,0,100};
int x = 1000;

Set_int s1 (int_1,int_1+5);
Set_int s2 (int_1,int_1+6);
Set_int s3;

It it=set_intersection(s1.begin(), s1.end(),\
s2.begin(), s2.end(),\
inserter(s3, s3.begin()));
//s3.erase(it,s3.end());
s3.insert(x);
for(It j=s3.begin();j!=s3.end();++j)
cout<<*j<<"\n";

return 0;

}
Best regards,
Davy

Jul 26 '05 #1
7 8066
Davy wrote:
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();
BTW, I use VC6.0(is it too old?).
YES - VC6 is broken with most template constructs. VC7 is significantly
better but still broken.

//---The code listed:---//
#include <math.h>
#include <fstream.h>
#include <algorithm>
#include <iterator>
#include <set>

using namespace std;
typedef set<int> Set_int;
typedef set<int>:: iterator It;

int main() {
int int_1[]={1,4,3,2,5};
int int_2[]={7,8,4,5,0,100};
int x = 1000;

Set_int s1 (int_1,int_1+5);
Set_int s2 (int_1,int_1+6); you probably meant int_2 here.
Set_int s3;

set_intersection returns whatever inserter(s3, s3.begin()) returns -
probably not an It.
It it=set_intersection(s1.begin(), s1.end(),\
s2.begin(), s2.end(),\
inserter(s3, s3.begin())); ....


The code below compiles and runs on gcc 4.0.

//---The code listed:---//
#include <cmath>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>

using namespace std;
typedef set<int> Set_int;
typedef Set_int::iterator It;

template <typename T, int N >
T * End( T (& i_array )[ N ] )
{
return N + i_array;
}

int main()
{
int int_1[]={1,4,3,2,5};
int int_2[]={7,8,4,5,0,100};
int x = 1000;

Set_int s1( int_1, End(int_1) );
Set_int s2( int_2, End(int_2) );
Set_int s3;

// It it =
set_intersection(
s1.begin(), s1.end(),
s2.begin(), s2.end(),
std::inserter(s3, s3.begin())
);

//s3.erase(it,s3.end());
// s3.insert(x);
for(It j=s3.begin();j!=s3.end();++j)
cout<<*j<<"\n";

}

Jul 26 '05 #2
foo


Davy wrote:
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();
BTW, I use VC6.0(is it too old?).

//---The code listed:---//
#include <math.h>
#include <fstream.h>
#include <algorithm>
#include <iterator>
#include <set>

using namespace std;
typedef set<int> Set_int;
typedef set<int>:: iterator It;

int main() {
int int_1[]={1,4,3,2,5};
int int_2[]={7,8,4,5,0,100};
int x = 1000;

Set_int s1 (int_1,int_1+5);
Set_int s2 (int_1,int_1+6);
Set_int s3;

It it=set_intersection(s1.begin(), s1.end(),\
s2.begin(), s2.end(),\
inserter(s3, s3.begin()));
//s3.erase(it,s3.end());
s3.insert(x);
for(It j=s3.begin();j!=s3.end();++j)
cout<<*j<<"\n";

return 0;

}

Davy,

I don't have a lot of time now, but study the following code,
and do ask questions - Someone will help you if I can't get
back soon enough ;-)

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

int main()
{
int A[ 4 ] = { 6, 26, 9, 14 };
int B[ 6 ] = { 5, 6, 15, 9, 20, 26 };

std::set<int> SA( A + 0, A + 4 );
std::set<int> SB( B + 0, B + 6 );

std::set<int> ResultSet;

std::insert_iterator<std::set<int> >
InsertIter( ResultSet, ResultSet.begin() );

std::cout << "Set SA: " << std::endl << " ";
std::copy( SA.begin(), SA.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << "\n\nSet SB: " << std::endl << " ";
std::copy( SB.begin(), SB.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cout << '\n' << std::endl;
std::set_intersection( SB.begin(), SB.end(),
SA.begin(), SA.end(), InsertIter );

std::cout << "Result:" << std::endl << " ";
std::copy( ResultSet.begin(), ResultSet.end(),
std::ostream_iterator<int>( std::cout, " " ) );

std::cin.get();
return 0;
}

-- OUTPUT --
Set SA:
6 9 14 26

Set SB:
5 6 9 15 20 26

Result:
6 9 26

Cheers,
Chris Val

Jul 26 '05 #3
"Davy" <zh*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@f14g2000cwb.googlegro ups.com...
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();
BTW, I use VC6.0(is it too old?).
VC6 is old and it has some nasty bugs, but it is not responsible for all
errors its users make.
//---The code listed:---//
#include <math.h>
#include <fstream.h>
#include <algorithm>
#include <iterator>
#include <set>
Why do you include all these headers. For this code you only need iostream,
set and algorithm.
using namespace std;
typedef set<int> Set_int;
typedef set<int>:: iterator It;

int main() {
int int_1[]={1,4,3,2,5};
int int_2[]={7,8,4,5,0,100};
int x = 1000;

Set_int s1 (int_1,int_1+5);
Set_int s2 (int_1,int_1+6);
Why do you initialize s1 and s2 with (almost the same) values? Also, int_1
only has 5 elements but you are telling the compiler to use 6 of them to
initialize s2. Anything might happen if you do such things.
Set_int s3;

It it=set_intersection(s1.begin(), s1.end(),\
s2.begin(), s2.end(),\
inserter(s3, s3.begin()));


set_intersection does not return a set iterator, but only an output
iterator. And why are you using line continuation. Even VC6 is smart enought
to recognize that a statement spans several lines.

HTH
Heinz
Jul 26 '05 #4
Hi all,

Thank you all for your help!

I changed It it=set_intersection(s1.begin(), s1.end(),\
s2.begin(), s2.end(),\
inserter(s3, s3.begin()));

to

set_intersection(s1.begin(), s1.end(),
s2.begin(), s2.end(),
inserter(s3, s3.begin()));

All passed.

And one more question:
And can I insert 2-D array like int[2] position = {x,y} as a element to
a Set, read it out, and delete it, what shall I notice?

Best regards,
Davy

Jul 26 '05 #5
"Davy" <zh*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g47g2000cwa.googlegr oups.com...
And can I insert 2-D array like int[2] position = {x,y} as a element to
a Set, read it out, and delete it, what shall I notice?


No. Arrays lack almost all requirements for elements to be but in any
standard container. Usually such elements must be assignable and
copy-constructable, which arrays are not. You cannot write (or at least the
compiler will not accept it)

int a[2] = { 1, 2 };
int b[2](a); // no copy-construction
b = a; // no assignment

But you can use a std::vector instead of an array, but you have to supply
your own comparison operator because operator< is not defined for vectors.

BTW: There are no 2-D arrays in C++, there are only 1-D arrays with elements
that are themselves 1-D arrays. An array with just two int's, however, is
not even an approximation of a 2-D array. Just having two elements does not
qualify for having two dimensions. An array (or vector) with to elements may
be used to specify points in a two dimensional space, but the array has only
one dimension (even if it had 3 'coordinates' to identify a point in three
dimensional space).

HTH
Heinz
Jul 26 '05 #6
Hi all,

Now I want to use
struct point2D
{
int x;
int y;

};
as the 2-D array subject.

But how to replace the "less than" function with '<'. Can you give me
some instructions?

Best regards,
Davy

Jul 27 '05 #7
Davy wrote:
Hi all,

Now I want to use
struct point2D
{
int x;
int y;

};
as the 2-D array subject.

But how to replace the "less than" function with '<'. Can you give me
some instructions?


define an operator.

inline bool operator<( const point2D & i_lhs, const point2D & i_rhs )
{
return ....;
}

Jul 27 '05 #8

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...
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,...
3
by: Generic Usenet Account | last post by:
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...
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...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.