Re: [STL] How to use set_intersection
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Mon, 25 Jul 2005 23:36:16 -0500
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++
.
- Follow-Ups:
- Re: How to use set_intersection
- From: zhushenli
- Re: How to use set_intersection
- References:
- [STL] How to use set_intersection
- From: zhushenli
- [STL] How to use set_intersection
- Prev by Date: [STL] How to use set_intersection
- Next by Date: Re: about std::string
- Previous by thread: [STL] How to use set_intersection
- Next by thread: Re: How to use set_intersection
- Index(es):
Relevant Pages
|
Loading