I'm trying to merge two sets of quartets (a class which I've defined).
Before I go into the merging, let me ask a question about operator overloading. If I want to overload the operator of a class, does it make a difference whether I do it within the class or outside the class? For instance, if I did it within the class, my function header would be
bool quartet::operator<(const quartet& other)
But if I did it outside of the class, my function header would be
bool operator<(const quartet& one, const quartet& two);
I've been switching between the two because I believe it eliminated some error in one place but created another, although I could not replicate them at the time of writing this.
OK, now onto merging. Right now the following code is working (where A,B,C, and D are defined above, and A==C)
set<quartet> Qset;
set<quartet> result;
Qset.insert(A);
Qset.insert(B);
set<quartet> Qset2;
Qset2.insert(C);
Qset2.insert(D);
merge(Qset.begin(), Qset.end(), Qset2.begin(), Qset2.end(), inserter(result, result.end()));
printSet(result);
So, my first question is, if I change the last parameter of merge from inserter(result, result.end())
to result.begin()
, I get the compiler error:
/usr/include/c++/4.6/bits/stl_algobase.h:299:6: error: passing ‘const quartet’ as ‘this’ argument of ‘quartet& quartet::operator=(const quartet&)’ discards qualifiers [-fpermissive]
make: *** [quartet.o] Error 1
Why do I get this error? My understanding is that the last parameter of merge takes an iterator to where the elements should be merged to, so why would that not be result.begin()? Furthermore, what exactly is inserter?
More generally, I'm eventually going to be working with large, sorted sets. Would it be faster to call merge or to call set_union? What is the difference between the two?
Finally, could I not just call set2.insert(set1.begin(), set1.end()) to merge the two sets together?
Thank you