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.
Code:
1. class Node
2. (
3. public:
4.
5. string libel;
6. list <Noeud *> son;
7.
8. bool operator <(const Node * c1)
9. (
10. return libel <c1-> libel;
11. )
12.
13. Node (string text)
14. (
15. libel = text;
16. )
17.
18. ajouterFils void (string text)
19. (
20. * NouveauNoeud node = new Node (text);
21. fils.push_front (nouveauNoeud);
22. )
23. void display (int NbTab)
24. (
25. if (! fils.empty ())
26. (
27. fils.sort ();
28. insererTab (NbTab);
29. court <<libel <<endl;
30. <Noeud*> list:: iterator it;
31. for (it = fils.begin (); it! fils.end = () + + it)
32. (
33. (* it) -> display (NbTab 1);
34. )
35. )
36. else
37. (
38. insererTab (NbTab);
39. court <<libel <<endl;
40. )
41. )
42.
43. );
44.
45.
46.
47. int main (void)
48. (
49. * Level 1 node = new Node ( "Level1");
50. * Level 4 Node = new Node ( "Level 4");
51.
52. level1-> ajouterFils ( "Niveau20");
53. level1-> ajouterFils ( "Niveau22");
54. level1-> ajouterFils ( "Niveau23");
55. level1-> ajouterFils ( "Niveau21");
56.
57. level1-> fils.front () -> ajouterFils ( "test2");
58. level1-> fils.front () -> ajouterFils ( "test1");
59. level1-> fils.front () -> ajouterFils ( "test3");
60.
61. level1-> display (0);
62. level1-> fils.sort ();
63. level1-> display (0);
64.
65. return 0;66. )
Here is the return to the console:
Code:
1. Level1
2. Niveau20
3. Niveau22
4. Niveau23
5. Niveau21
6. test2
7. test1
8. test3
9. Level1
10. Niveau20
11. Niveau22
12. Niveau23
13. Niveau21
14. test2
15. test1
16. test3
17. Press any key to continue
Bookmarks