Thread: set_intersection of two sets

  1. #1
    Registered User
    Join Date
    May 2008

    set_intersection of two sets

    How to find the set intersection of two sets using STL algorithm?

    set<int> s1;
    set<int> s2;
    //& want to save it in set s i.e. s = s1^s2
    set<int> s;
    //i.e. s=?
    Last edited by kapil1089thekin; 08-11-2010 at 03:38 PM.

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Plano, Texas, United States
    Check here for an example:

    set_intersection - C++ Reference
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    May 2008
    It gives the number of the element in the given example.. I want to get the intersected set.

  4. #4
    The larch
    Join Date
    May 2006
    It gives the intersected set. The example just uses it only to display its size.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2008
    it gives the intersected set but the size of v doesn't change with the intersection size.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    You're concerned that v may be too large? Then may I suggest using a back insert iterator to put elements of the intersected set at the end of the vector:
      #include <iterator>
      vector<int> v;
      set_intersection (first, first+5, second, second+5, back_inserter(v));
      cout << "intersection has " << v.size() << " elements.\n";
    Output iterators usually start at the beginning, which will overwrite elements instead of letting the vector (or whatever) grow naturally. See back_insert_iterator - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of sets
    By kapil1089thekin in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2010, 03:12 PM
  2. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 06:37 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM