TITLE: set_difference in STL (Newsgroups: comp.std.c++, 17 Sep 97) MANGINO: mangino@saturn.planet.net (Reed Mangino) 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 sentence 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 #include #include #pragma warning(disable: 4786) using namespace std; int main() { set SrcSet1; set SrcSet2; set 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::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?? BECKER: Pete Becker 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 #include int main() { vector v1; vector v2; vector 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.