algorithm: set_difference 
Author Message
 algorithm: set_difference

I tried using the set_difference template function with a
set container, but I get compiler errors.  I read in TC++,
18.7.5, that set operations work well with sets and
multisets.  So, I know I am missing something basic.

Here is my source code followed by my compiler error.  To
me, it looks like the compiler error saying the increment
operation for set is not supported?  Any advice or
suggestions are appreciated.

Thanks,
James

#include <set>
#include <algorithm>

int main(void)
{
        std::set<int> myset1;
        myset1.insert(1);
        std::set<int> myset2;
        myset2.insert(1);
        myset2.insert(2);

        std::set<int> myset3;

        std::set_difference(myset1.begin(),myset1.end
(),myset2.begin(),myset2.end(),myset3);

        return 0;

Quote:
}

c:\program files\microsoft visual studio\vc98
\include\algorithm(1202) : error C2676:
binary '++' : 'class std::set<int,struct
std::less<int>,class std::allocator<int> >' does not
define this operator or a conversion to a type acceptable
to the prede
fined operator


Sun, 03 Apr 2005 22:09:30 GMT  
 algorithm: set_difference

Quote:
> I tried using the set_difference template function with a
> set container, but I get compiler errors.  I read in TC++,
> 18.7.5, that set operations work well with sets and
> multisets.  So, I know I am missing something basic.
<snip>

> std::set_difference(myset1.begin(),myset1.end
> (),myset2.begin(),myset2.end(),myset3);

The final parameter of set_difference needs to be an output iterator,
not a set.  Try "inserter(myset3,myset3.end())".

Note that the algorithm works with any sorted container, not just
std::set.

Ken



Sun, 03 Apr 2005 22:46:50 GMT  
 algorithm: set_difference

Quote:

>         std::set_difference(myset1.begin(),myset1.end
> (),myset2.begin(),myset2.end(),myset3);

Algorithms take iterators. myset3 is not an iterator. Read about
std::back_inserter.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Sun, 03 Apr 2005 22:49:36 GMT  
 algorithm: set_difference
#include <iostream>
#include <set>
#include <algorithm>

int main(int argc, char* argv[])
{
    std::set<int> myset1,myset2,myset3;

    myset1.insert(1);
    myset1.insert(2);
    myset2.insert(1);

    std::set_difference(myset1.begin(),myset1.end(),
        myset2.begin(),myset2.end(), std::inserter(myset3,myset3.begin()));

    for (std::set<int>::iterator it = myset3.begin(); it != myset3.end();
it++)
    {
        std::cout << *it << std::endl;
    }

    return 0;

Quote:
}



Quote:
> I tried using the set_difference template function with a
> set container, but I get compiler errors.  I read in TC++,
> 18.7.5, that set operations work well with sets and
> multisets.  So, I know I am missing something basic.



Sun, 03 Apr 2005 22:55:38 GMT  
 algorithm: set_difference

Quote:

> >         std::set_difference(myset1.begin(),myset1.end
> > (),myset2.begin(),myset2.end(),myset3);

> Algorithms take iterators. myset3 is not an iterator. Read about
> std::back_inserter.

std::back_inserter won't work if he's storing the results in a set (as
his sample was attempting to do).

That said, if the James doesn't plan on using the dynamic set-ness of
his output (ie, inserting, deleting, and querying), but intends instead
to use results as input to another set algorithm or just to read out the
results in order, vector will be the more efficient solution.

Ken



Mon, 04 Apr 2005 06:08:17 GMT  
 algorithm: set_difference

Quote:




> > >         std::set_difference(myset1.begin(),myset1.end
> > > (),myset2.begin(),myset2.end(),myset3);

> > Algorithms take iterators. myset3 is not an iterator. Read about
> > std::back_inserter.

> std::back_inserter won't work if he's storing the results in a set (as
> his sample was attempting to do).

> That said, if the James doesn't plan on using the dynamic set-ness of
> his output (ie, inserting, deleting, and querying), but intends instead
> to use results as input to another set algorithm or just to read out the
> results in order, vector will be the more efficient solution.

Which is why it's important to read about back_inserter. (Good point,
though: I didn't pay attention to the set aspect)

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Mon, 04 Apr 2005 06:16:25 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to set_difference map and list

2. Comments on "Algorithm in C" or "Algorithm in C++"

3. Modulo 11, check digit generation algorithm or code wanted. (prefer algorithm).

4. Sorting, Algorithms, Data Structure, and Libraries

5. Need algorithm

6. Link-list sort algorithm question

7. MD5 and SHA-1 algorithm

8. Help with an algorithm in C

9. Most significant bit algorithm

10. Looking for an algorithm

11. inductive algorithm for permutations - help needed

12. s:8-Queens and N-Queens algorithms (2)

 

 
Powered by phpBB® Forum Software