set_intersection STL, help please!

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Emilio
Experienced poster
Posts: 163
Joined: Sun Oct 17, 2004 8:31 pm
Location: Murcia, Spain

set_intersection STL, help please!

Post by Emilio » Wed Dec 21, 2005 5:11 am

Hello there!

I'm trying intersect two STL sets but I can't achieve it.
In the next code I show how I'm trying it, I want the intersection of set1 and set2. How can get it in another set(set3 in code)?
Any example will be highly-regarded!
I have tried other examples, for example http://www.sgi.com/tech/stl/set.html or http://www.sgi.com/tech/stl/set_intersection.html
Neither the next code nor the two examples above compile with my compiler (Dev-C++).
Thanks!

Code: Select all

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cmath>
#include <cassert>
#include <list>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

int main ()
{
    set<int> set1, set2, set3;
    
    set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), set3.begin());
}

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Wed Dec 21, 2005 9:16 am

The fifth argument must be an output iterator. You can't use just set3.begin(), you have to replace it with insert_iterator<set<int> >(set3, set3.begin()).

Anyway, that's exactly what compiler says to do.

As for the SGI examples - they do compile if you include the right headers (algorithm, iostream, iterator).

Emilio
Experienced poster
Posts: 163
Joined: Sun Oct 17, 2004 8:31 pm
Location: Murcia, Spain

Post by Emilio » Wed Dec 21, 2005 2:49 pm

Thanks Krzysztof!

I didn't know iterator header.

Post Reply

Return to “C++”