Topic: set_difference usage


Author: Matt Austern <austern>
Date: 1997/09/25
Raw View
Pete Becker <petebecker@acm.org> writes:

> One thing that often confuses folks is that the container named 'set'
> has nothing to do with the algorithms that deal with sets. The
> algorithms perform set operations like union, intersection, etc. on
> ordered containers. When you're first figuring out how these things
> work, stick to something like vector:

A good point.  On the other hand, the set algorithms aren't completely
unrelated to the set<> container, since set<> always provides an
ordered range and since it provides an efficient means of inserting
ordered ranges.  You don't have to use the set algorithms together
with set<> (Pete's suggestion of vector<> is a reasonable one), but
they're a good match.

So, for example, suppose that S1 and S2 are both of type set<int>.
You can form a new set<> containing their union by writing the
following.
    set<int> result;
    set_union(S1.begin(), S1.end(),
              S2.begin(), S2.end(),
              inserter(result, result.begin());
This is a linear-time operation.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Pete Becker <petebecker@acm.org>
Date: 1997/09/23
Raw View
Reed Mangino wrote:
>
> I am attempting to use set_difference to build a new set based upon
> the difference between to other sets.
>
> The last parameter of set_difference takes an Output Interator (like
> ostream_iterator).  But, what if I want to do what my first sentance
> states??
>
> The code that I thought would do the trick is shown below.  But my
> compiler returns:
> "C:\Program Files\DevStudio\VC\INCLUDE\algorithm(1195) : error C2166:
> l-value specifies const object".
>
> Here is the code:
>
> If this make no sense (because of the semantics of output iterators),
> how _should_ I do this??

One thing that often confuses folks is that the container named 'set'
has nothing to do with the algorithms that deal with sets. The
algorithms perform set operations like union, intersection, etc. on
ordered containers. When you're first figuring out how these things
work, stick to something like vector:

#include <vector>
#include <algorithm>

int main()
{
 vector<int> v1;
 vector<int> v2;
 vector<int> v3;

        v1.insert(1);
        v1.insert(2);
        v1.insert(3);

        v2.insert(3);
        v2.insert(4);
        v2.insert(5);

 sort( v1.begin(), v1.end() );
 sort( v2.begin(), v2,end() );

        set_difference(v1.begin(),
                       v1.end(),
                       v2.begin(),
                       v2.end(),
                       inserter(v3) );

        return 0;
}

Note also that I removed DestBeg. In general you'll be much better off
if you avoid creating named iterators. Instead, create them on the fly
as function arguments. That way you don't have to worry about whether
DestBeg is still valid after you've done a bunch of container
manipulations.
 -- Pete
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: mangino@saturn.planet.net (Reed Mangino)
Date: 1997/09/17
Raw View
I am attempting to use set_difference to build a new set based upon
the difference between to other sets.

The last parameter of set_difference takes an Output Interator (like
ostream_iterator).  But, what if I want to do what my first sentance
states??

The code that I thought would do the trick is shown below.  But my
compiler returns:
"C:\Program Files\DevStudio\VC\INCLUDE\algorithm(1195) : error C2166:
l-value specifies const object".

Here is the code:

// MSVC++ 5.0
#include <iostream>
#include <set>
#include <algorithm>

#pragma warning(disable: 4786)

using namespace std;

int main()
{
 set<int> SrcSet1;
 set<int> SrcSet2;
 set<int> DestSet; // will contain difference of SrcSet1 and
SrcSet2

 SrcSet1.insert(1);
 SrcSet1.insert(2);
 SrcSet1.insert(3);

 SrcSet2.insert(3);
 SrcSet2.insert(4);
 SrcSet2.insert(5);

 set<int>::iterator DestBeg = DestSet.begin();

 set_difference(SrcSet1.begin(),
         SrcSet1.end(),
          SrcSet2.begin(),
         SrcSet2.end(),
         DestBeg);

 return 0;
}

If this make no sense (because of the semantics of output iterators),
how _should_ I do this??

Thanks!
Reed

--

    ^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
     Reed R. Mangino       |  ** Dialogic Corporation **
     manginor@dialogic.com | World leader in the design of
     mangino@planet.net    |  computer telephony systems
   -----------------------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Brock Peabody <npcis@pitton.com>
Date: 1997/09/18
Raw View

,,, < snip >
>         set_difference(SrcSet1.begin(),
>                        SrcSet1.end(),
>                        SrcSet2.begin(),
>                        SrcSet2.end(),
>                        DestBeg);
>
>         return 0;
> }

Set_difference expects its last argument to be an insert iterator.  The
type of iterator returned by begin() is not, so you must construct one.

try this instead :

 set_difference(SrcSet1.begin(),
   SrcSet1.end(),
   SrcSet2.begin(),
   SrcSet2.end(),
   insert_iterator<set<int> >        DestSet,DestSet.begin())
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1997/09/18
Raw View
mangino@saturn.planet.net (Reed Mangino) wrote:

// :  set<int>::iterator DestBeg = DestSet.begin();
:
:  set_difference(SrcSet1.begin(),
:          SrcSet1.end(),
:           SrcSet2.begin(),
:          SrcSet2.end(),
// How about
               inserter(DestSet, DestSet.begin()));
// :          DestBeg);

John
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]