Re: [STL] How to use set_intersection



On 25 Jul 2005 20:58:13 -0700, zhushenli@xxxxxxxxx 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?).

VC6 is indeed pretty old. If you're just learning, the VC2005 Express
Edition beta will suit you better:

http://lab.msdn.microsoft.com/express/visualc/default.aspx

> //---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 meant to use int_2 in the initialization of s2. (Your current code runs
off the end of int_1.)

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

You can get rid of those line continuation characters.

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

It may be a tiny bit more efficient and certainly more conventional to use
the character literal '\n' instead of the string literal "\n".

> return 0;
>
> }

I don't see how that could've compiled under VC6, whose STL doesn't
implement the member template ctors you're using. In addition, the type
returned by your usage of set_intersection isn't compatible with "It".
Thus, I don't see how it ever compiled successfully. If you provide details
concerning your setup (compiler version, service pack, other libraries
you're using), the command line you used to compile this program, and the
warning you got, someone may be able to help you. In the meantime, here's a
corrected program that compiles and runs under VC7.1:

#include <algorithm>
#include <iostream>
#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_2,int_2+6);
Set_int s3;

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

return 0;

}

C>cl -GX -W3 b.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

b.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:b.exe
b.obj

C>b
4
5
1000

--
Doug Harrison
Microsoft MVP - Visual C++
.



Relevant Pages

  • Re: Parameter Name Warning?
    ... the names on the declarations do not matter, it should perhaps give a warning regarding different names being used. ... > there is more to those names than just what the compiler does with them. ... > You know only what you get in the header. ... >>> int main ...
    (microsoft.public.vc.language)
  • Re: is order urgent doubt
    ... Which *compiler* are you using? ... layout of the warning messages.) ... i = sizeof(long int); ... about the code in the editor vs. the code in the source file. ...
    (comp.lang.c)
  • Re: Whats the deal with size_t?
    ... ES> allowed to emit diagnostics that C doesn't require. ... in warning about a signed to unsigned conversion. ... a size_t to an int it is quite justified in warning about the reverse, ... The compiler is also quite justified in warning if you use a signed ...
    (comp.lang.c)
  • Re: Compendiums of compiler warning/error messages mapped to actual code problems?
    ... The general problem is of course that the compiler messages must be short, and that tends to make them so cryptic that it isn't always immediately obvious what the problem is. ... int this; ... Warning twarn.c: ...
    (comp.lang.c)
  • Re: matrix stuff (solving b = A*x) --> using numerical recipes
    ... but then I removed it since I couldn't see any compiler warnings/errors without stdio.h). ... turn the warning level up to the maximum ... void banmul(float **a, unsigned long n, int left, int right, float x, float b); ...
    (comp.lang.c)

Loading