Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL-set difference and intersection

STL-set difference and intersection

Scheduled Pinned Locked Moved ATL / WTL / STL
c++dockertutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liquid_
    wrote on last edited by
    #1

    How to compute difference and intersection of two sets? There are functions set_difference and set_intersection which requires two pairs of iterators of source sets and output iterator of resulting set. Here, set can be any sorted container with incrementable iterator. There is another requirement that the output should be large enough to contain the destination range. For STL-set, however, you cannot set the size of it and using an empty set as output leads to assertion. In fact, output iterator of destination empty set is equal to end() and incrementing iterator does not add elements to a set. So how to compute this?

    S 1 Reply Last reply
    0
    • L liquid_

      How to compute difference and intersection of two sets? There are functions set_difference and set_intersection which requires two pairs of iterators of source sets and output iterator of resulting set. Here, set can be any sorted container with incrementable iterator. There is another requirement that the output should be large enough to contain the destination range. For STL-set, however, you cannot set the size of it and using an empty set as output leads to assertion. In fact, output iterator of destination empty set is equal to end() and incrementing iterator does not add elements to a set. So how to compute this?

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Use std::inserter[^] or std::back_inserter[^] for the output iterator. That causes the items to be inserted into the container without having to know what size it should be before you've constructed it. If you're using a vector or list as the output, I'd use std::back_inserter. For a set, std::inserter is required. Here's an example:

      #include <vector>
      #include <set>
      #include <iostream>
      #include <algorithm>
      #include <iterator>

      int main()
      {
      std::set<int> a, b, c;
      a.insert(1);
      a.insert(2);
      a.insert(3);
      a.insert(4);
      b.insert(2);
      b.insert(4);
      b.insert(5);
      b.insert(7);
      std::vector<int> d;

      std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::inserter(c, c.end()));
      std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(d));

      std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, " "));
      std::copy(d.begin(), d.end(), std::ostream_iterator<int>(std::cout, " "));
      }

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      L 1 Reply Last reply
      0
      • S Stuart Dootson

        Use std::inserter[^] or std::back_inserter[^] for the output iterator. That causes the items to be inserted into the container without having to know what size it should be before you've constructed it. If you're using a vector or list as the output, I'd use std::back_inserter. For a set, std::inserter is required. Here's an example:

        #include <vector>
        #include <set>
        #include <iostream>
        #include <algorithm>
        #include <iterator>

        int main()
        {
        std::set<int> a, b, c;
        a.insert(1);
        a.insert(2);
        a.insert(3);
        a.insert(4);
        b.insert(2);
        b.insert(4);
        b.insert(5);
        b.insert(7);
        std::vector<int> d;

        std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::inserter(c, c.end()));
        std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(d));

        std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, " "));
        std::copy(d.begin(), d.end(), std::ostream_iterator<int>(std::cout, " "));
        }

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        L Offline
        L Offline
        liquid_
        wrote on last edited by
        #3

        Thanks a lot. It works.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups