473,320 Members | 2,048 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,320 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 8060
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.