Re: Set operations.

Discussion in 'C++' started by Peter Gregory, Jul 16, 2003.

  1. Peter Gregory wrote:

    > Hi,
    >
    > I wanted to perform set operations (union, intersect) on STL sets.
    > The set_union and set_intersection functions work okay but they
    > require the target iterator's container to be of the correct size
    > to put the resultant items into. I obviously don't know this
    > number. I wrote the following function to deal with union:
    >
    >
    > template <class _Input1, class _Input2, class _Output>
    > void myset_union(_Input1 a, _Input2 b, _Output& c) {
    >
    > c.resize( max(a.size(),b.size()) );
    >
    > int newSize = distance( c.begin(),
    > set_union ( a.begin(), a.end(),
    > b.begin(), b.end(),
    > c.begin() )
    > );
    >
    > c.resize(newSize);
    >
    > }
    >
    >
    > which works fine on vector and list, but set doesn't have a
    > resize() function. Is there another way to get the
    > union/intersection of two sets and put them in a new set?
    >
    > Thanks,
    > Pete.


    Perhaps I should have included compilable code, sorry.

    //myalgo.h

    #include <algorithm>

    template <class _Input1, class _Input2, class _Output>
    void myset_union(_Input1 a, _Input2 b, _Output& c) {

    c.resize( max(a.size(),b.size()) );

    int newSize = distance( c.begin(),
    set_union ( a.begin(), a.end(),
    b.begin(), b.end(),
    c.begin() )
    );

    c.resize(newSize);

    }


    //main.cpp
    #include <iostream.h>
    #include <list>
    #include <algorithm>
    #include <string>
    #include "myalgo.h"

    using namespace std;

    int main()
    {

    list<string> bob;
    list<string> bob2;
    list<string> bob3;

    bob.push_back("arthur");
    bob.push_back("bob");
    bob.push_back("james");
    bob.push_back("bob");
    bob.push_back("george");

    bob2.push_back("bob");
    bob2.push_back("james");

    myset_union( bob,bob2,bob3 );

    for (list<string>::iterator i = bob3.begin();
    i != bob3.end();
    i++)
    {
    string s(*i);
    cout << s << endl;
    }

    return 0;
    };

    This works fine except duplicates are included that I don't want.
    So my logic followed that I should use sets not lists but you
    can't resize a set so my template function wont work then.

    If anyone knows the correct way to do this or even if there is no
    standard way then feedback would be much appreciated,

    thanks,
    Pete
    Peter Gregory, Jul 16, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,160
    robert
    Feb 11, 2006
  2. Robert W Hand

    Re: Set operations.

    Robert W Hand, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    363
    Robert W Hand
    Jul 16, 2003
  3. Replies:
    2
    Views:
    266
    Diez B. Roggisch
    Oct 20, 2005
  4. George Sakkis

    Set operations in Numeric

    George Sakkis, Oct 23, 2006, in forum: Python
    Replies:
    2
    Views:
    246
    George Sakkis
    Oct 23, 2006
  5. Generic Usenet Account

    STL :: Set operations on sorted structures

    Generic Usenet Account, Nov 23, 2005, in forum: C++
    Replies:
    8
    Views:
    333
    Generic Usenet Account
    Dec 7, 2005
Loading...

Share This Page