15,949,686 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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.
http://www.cplusplus.com/reference/list/list/
#include <iostream>
#include <list>
using namespace std;
class MyClass
{
public:
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");
mylist.push_back(mc1);
mylist.push_back(mc2);
mylist.push_back(mc3);
cout << "before deleting:" << endl;
for (iter = mylist.begin(); iter != mylist.end(); iter++)
{
cout << (*iter).a << " " << (*iter).b << " " << (*iter).c << endl;
}
mylist.remove(mc3);
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?