|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Sorting a list of type class I have declared a list from the STL. This is a list of the type of class. But I am unable to work out my operator: Code: class Node { public: string label; list<Node*> child; bool operator< (const Node *c1) { return label < c1->label; } Node (string text) { label=text; } void addChild(string text) { Node* newNode = new Node (text); child.push_front(newNode); } void display(int NbTab) { if (!child.empty()) { child.sort(); insertTab(NbTab); cout<<label<<endl; list<Node*>::iterator it; for (it=child.begin(); it!=child.end(); ++it) { (*it)->display(NbTab+1); } } else { insertTab(NbTab); cout<<label<<endl; } } }; int main(void) { Node* level1=new Node ("Level1" ); Node* level4=new Node ("Level4" ); level1->addChild("Level20" ); level1->addChild("Level22" ); level1->addChild("Level23" ); level1->addChild("Level21" ); level1->child.front()->addChild("test2" ); level1->child.front()->addChild("test1" ); level1->child.front()->addChild("test3" ); level1->display(0); level1->child.sort(); level1->display(0); return 0; } Quote:
|
#2
| |||
| |||
Re: Sorting a list of type class How does nothing happens? It sorts according to your pointers, as you probably ask him. A list of pointers will be sorted with the order defined on pointers, not objects points if you do not explicitly specify the comparison function to use. |
#3
| |||
| |||
Re: Sorting a list of type class You can call out to passing it as argument may be, its the predicate that will make the comparison as you want between nodes Code: bool pred( const Node *l, const Node *r ) { return l->label < r->label; } // ... level1->child.sort(pred);
__________________ The FIFA Manager 2009 PC Game |
#4
| |||
| |||
Re: Sorting a list of type class @ const: How can I specify this function to sort my list as per my requirement? @ opaper: thank you for enlightening me, I confess that I passed but I think you've noticed I already try the same thing, the problem is that I can not use it in my class node (in any case I failed) |
#5
| |||
| |||
Re: Sorting a list of type class The predicate should not be a member function of your class or it is necessary that he be declared as static member. Always see what you try and what errors do you get, it will be quicker to help.
__________________ The FIFA Manager 2009 PC Game |
![]() |
|
Tags: class type, cpp, list, sort |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Confused about the List class | Jagruti23 | Software Development | 5 | 05-03-2010 04:17 PM |
Abstract class unable to implement List<T> | MAHESA | Software Development | 5 | 02-03-2010 09:00 PM |
Creating a vector type of a class | Xmen | Software Development | 5 | 24-02-2010 03:38 AM |
Problem in List-style-type property | Juany | Software Development | 5 | 23-02-2010 04:17 PM |
Is there a master list of Microsoft Windows Server 2003 services that will also list what the default startup type? | Spin | Windows Server Help | 1 | 21-09-2008 09:05 AM |