Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 26th, 2005, 05:05 AM
Davy
Guest
 
Posts: n/a
Default [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

  #2  
Old July 26th, 2005, 05:55 AM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: [STL] How to use set_intersection

Davy wrote:[color=blue]
> 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?).[/color]

YES - VC6 is broken with most template constructs. VC7 is significantly
better but still broken.
[color=blue]
>
> //---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);[/color]
you probably meant int_2 here.
[color=blue]
> Set_int s3;
>[/color]

set_intersection returns whatever inserter(s3, s3.begin()) returns -
probably not an It.
[color=blue]
> It it=set_intersection(s1.begin(), s1.end(),\
> s2.begin(), s2.end(),\
> inserter(s3, s3.begin()));[/color]
....[color=blue]
>[/color]

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";

}

  #3  
Old July 26th, 2005, 06:05 AM
foo
Guest
 
Posts: n/a
Default Re: How to use set_intersection



Davy wrote:[color=blue]
> 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;
>
> }[/color]


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

  #4  
Old July 26th, 2005, 07:45 AM
Heinz Ozwirk
Guest
 
Posts: n/a
Default Re: [STL] How to use set_intersection

"Davy" <zhushenli@gmail.com> schrieb im Newsbeitrag
news:1122350307.532395.75590@f14g2000cwb.googlegro ups.com...[color=blue]
> 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?).[/color]

VC6 is old and it has some nasty bugs, but it is not responsible for all
errors its users make.
[color=blue]
> //---The code listed:---//
> #include <math.h>
> #include <fstream.h>
> #include <algorithm>
> #include <iterator>
> #include <set>[/color]

Why do you include all these headers. For this code you only need iostream,
set and algorithm.
[color=blue]
> 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);[/color]

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.
[color=blue]
> Set_int s3;
>
> It it=set_intersection(s1.begin(), s1.end(),\
> s2.begin(), s2.end(),\
> inserter(s3, s3.begin()));[/color]

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


  #5  
Old July 26th, 2005, 03:45 PM
Davy
Guest
 
Posts: n/a
Default Re: How to use set_intersection

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

  #6  
Old July 26th, 2005, 08:15 PM
Heinz Ozwirk
Guest
 
Posts: n/a
Default Re: How to use set_intersection

"Davy" <zhushenli@gmail.com> schrieb im Newsbeitrag
news:1122388577.829305.174230@g47g2000cwa.googlegr oups.com...
[color=blue]
> 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?[/color]

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


  #7  
Old July 27th, 2005, 03:55 AM
Davy
Guest
 
Posts: n/a
Default Re: How to use set_intersection

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

  #8  
Old July 27th, 2005, 06:15 AM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: How to use set_intersection

Davy wrote:[color=blue]
> 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?[/color]

define an operator.

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles