Re: Set operations.

Discussion in 'C++' started by Robert W Hand, Jul 16, 2003.

  1. On Wed, 16 Jul 2003 12:15:25 +0000, 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:


    You need to use an inserter function from <iterator>. The code below
    provides an example of its use. For more detail, Matt Austern
    provides a nice explanation of these issues in his book.

    std::string st1[]={"one", "two", "three", "four"};
    std::set<std::string> s1(st1, st1+sizeof st1/sizeof st1[0]);
    std::string st2[]={"one", "two", "five", "six"};
    std::set<std::string> s2(st2, st2 + sizeof st2/sizeof st2[0]);
    std::set<std::string> s3;

    std::set_union(s1.begin(), s1.end(),
    s2.begin(), s2.end(),
    std::inserter(s3, s3.begin()));

    std::copy(s3.begin(), s3.end(),
    std::eek:stream_iterator<std::string>(std::cout, " "));

    Output:
    five four one six three two



    Best wishes,

    Bob
     
    Robert W Hand, 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,247
    robert
    Feb 11, 2006
  2. Peter Gregory

    Re: Set operations.

    Peter Gregory, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    354
    Peter Gregory
    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:
    247
    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:
    336
    Generic Usenet Account
    Dec 7, 2005
Loading...

Share This Page