By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

[STL] How to use set_intersection

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.