[STL] How to use set_intersection

D

Davy

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
 
G

Gianni Mariani

Davy said:
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";

}
 
F

foo

Davy said:
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::eek:stream_iterator<int>( std::cout, " " ) );

std::cout << "\n\nSet SB: " << std::endl << " ";
std::copy( SB.begin(), SB.end(),
std::eek:stream_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::eek:stream_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
 
H

Heinz Ozwirk

Davy said:
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
 
D

Davy

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
 
H

Heinz Ozwirk

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
 
D

Davy

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
 
G

Gianni Mariani

Davy said:
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 ....;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

2 JK Circuit in VHDL 0
set_intersection 4
Strange errors about set using STL 4
How to use itoa() in C++? 4
Modify STL multiset 2
STL set Problem 5
pls help me ; vhdl; 0
set of structs (intersection) 1

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top