Click here to Skip to main content
15,921,371 members

Comments by Cashey Wen (Top 4 by date)

Cashey Wen 12-Dec-12 5:42am View    
I think you might be inserting 4 duplicated elements. STL set automatically removes all duplicated elements, this guarantees elements stored are unique.

According to your classcomp, if lhs.getID() is equal to rhs.getID(), the set will regard lhs and rhs as two duplicated elements, only one of them will be stored.

If you have to store dumplicated elements, set is not the solution. Sorry for my mistake, I thought "delete a *specific* element from a set" means a set with no dumplicated elements.

Well, if it is a question like this, this could be easier. Just use list, I got an example for you.

#include <iostream>
#include <list>
using namespace std;

class MyClass
MyClass(int _a, int _b, string _c): a(_a), b(_b), c(_c){}
bool operator ==(MyClass mc)
if (a == mc.a && b == mc.b && c == mc.c)
return true;
return false;

int a;
int b;
string c;

int main()
list<myclass> mylist;
list<myclass>::iterator iter;

MyClass mc1(1, 2, "abc");
MyClass mc2(2, 3, "ccc");
MyClass mc3(3, 4, "bbb");


cout << "before deleting:" << endl;
for (iter = mylist.begin(); iter != mylist.end(); iter++)
cout << (*iter).a << " " << (*iter).b << " " << (*iter).c << endl;

cout << "after deleting:" << endl;
for (iter = mylist.begin(); iter != mylist.end(); iter++)
cout << (*iter).a << " " << (*iter).b << " " << (*iter).c << endl;

cin >> ws;
return 0;
Cashey Wen 9-Dec-12 21:03pm View    
Hi, thought the order of elements doesn't matter for you, it does matter for STL.
The STL set uses a "r-b tree" to store all the elements, when you insert an element, the set have to compare the new element with all existing element.

The assertion should have told you a file name and a line number about where it happened, you can check it up at the corresponding file, if you do that, you'll probably find sth like this:

if (!_Pred(_Left, _Right))
return (false);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return (true);

The code shown above is from the header algorithm. This code snippet use the overloaded operator < to check if _Left is smaller than _Right, if _Left is smaller, it will swap the two operand to check if _Right is smaller than _Left.

If _Left is smaller than _Right, and _Right is also smaller than _Left, it will assert your < operator is invalid.

So if you still want to use set, you have to provide a valid < operator, you can simply return the compare result of one of the CustomClass's members.

struct classcomp {

bool operator() (const CustomClass& lhs, const CustomClass& rhs) const
return lhs.smdata < rhs.smdata; // smdata is a member of CustomClass in this case
Cashey Wen 9-Dec-12 12:31pm View    
Runtime error? You mean your code got compiled with no error but when it runs error happend?
If the compiler shows "invalid operator <", it is possible you didn't write the classcomp in a right way, and that's not runtime error.

Can you let me see some of your code?
Cashey Wen 9-Mar-12 22:44pm View    
Since tried VC6, why not change the linker?