Results 1 to 11 of 11

Thread: std:: list liste.sort ();

  1. #1
    Mike Gatting is offline Member
    Join Date
    May 2009
    Posts
    74
    Rep Power
    4

    Default std:: list liste.sort ();

    I have declared a list from the STL. This is a list of the type of class.

    ===> List <NomDeClasse> list;

    I do a lot of this list thanks to the Sort method provided in the class list of c + + builder.

    How can the list be sorted according to a field (string) being in my class.

    I was told it was overloading the operator <but I do not see how!

    Thank you for helping me.

  2. #2
    Join Date
    May 2009
    Posts
    81
    Rep Power
    4

    Default

    You can define:

    Code:
    1.	struct (MyGreater
    2.	bool operator () (const ClassName & c1, const ClassName & c2)
    3.	(
    4.	return c1.champ> c2.champ;
    5.	);6.	)
    After that

    Code:
    1.	liste.sort (MyGreater ());

  3. #3
    David Gower is offline Member
    Join Date
    May 2009
    Posts
    75
    Rep Power
    4

    Default

    And to make it in reverse order
    MyGreater you replace the inverse function

    A +

  4. #4
    Join Date
    May 2009
    Posts
    87
    Rep Power
    4

    Default

    There is no method std:: list:: sort in STL all confused.

    used std:: sort

  5. #5
    Ian Botham is offline Member
    Join Date
    Apr 2009
    Posts
    83
    Rep Power
    4

    Default

    you can not use sort of algorithm on lists, but you probably ought to know that.

  6. #6
    Bob Willis is offline Member
    Join Date
    Apr 2009
    Posts
    92
    Rep Power
    4

    Default

    Yes I misspoke and that's not true that I have eyes in front of the hole after 7 games in a row War3.

  7. #7
    Join Date
    Apr 2009
    Posts
    73
    Rep Power
    4

    Default

    Hi, dsl to determine the subject but I have a very similar problem. Unable to work out my operator:

    Code:
    1.	class Node
    2.	(
    3.	public:
    4.	string libel;
    5.	list <Noeud *> son;
    6.	
    7.	bool Node:: operator <(const Node & n) / / returns true if the libel of me meme is located before the wording of N
    8.	(
    9.	return libel <N.libelle;
    10.	)11.	...
    when I use spell on my list <Noeud *> it nothing happens.

    Could someone tell me what I miss?
    thank you

  8. #8
    Mike Brearley is offline Member
    Join Date
    Apr 2009
    Posts
    91
    Rep Power
    4

    Default

    How does it, it nothing happens? It sorts according to your pointers, as you probably ask him.

  9. #9
    Tony Greig is offline Senior Member
    Join Date
    Apr 2009
    Posts
    113
    Rep Power
    4

    Default

    Nothing is happening, because it is not clear.

    I have a list <Noeud *> I filled (with libel in a disorder Alphabetically). I display my list -> everything is OK it is in disorder (order in which I returned).

    I then re-released poster -> order is always the same of it or nothing happens.

    Because the AC is running out of useful info to really see the problem.

  10. #10
    Alan Knott is offline Senior Member
    Join Date
    Apr 2009
    Posts
    141
    Rep Power
    4

    Default

    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

  11. #11
    Mike Denness is offline Senior Member
    Join Date
    Apr 2009
    Posts
    105
    Rep Power
    4

    Default

    you can call out to passing it as argument a predicate is the predicate that will make the comparison as you want between nodes

    Edit, just to chew you work if my explanation is not clear enough:

    Code:
    1.	bool pred (const Node * l, const Node * r)
    2.	(
    3.	return l-> label <r-> libel;
    4.	)
    5.	/ / ...
    6.	level1-> fils.sort (pred);
    Incidentally, you nodes that your sort is not deep, you can sort the son of a node, not the small son.

    Edit: const

Similar Threads

  1. Sort-of-pronounceable-password generator
    By ABEDNEGO87 in forum Linux/Free BSD
    Replies: 0
    Last Post: 05-28-2009, 10:36 AM
  2. Sort your data
    By Alan Knott in forum Windows XP
    Replies: 0
    Last Post: 04-28-2009, 04:30 AM
  3. Sort Data In A Protected Worksheet
    By poulharis in forum Everything Else
    Replies: 0
    Last Post: 08-06-2008, 02:31 PM
  4. How Sort out the Excel
    By jordan in forum Everything Else
    Replies: 0
    Last Post: 05-19-2008, 12:35 PM
  5. Sort Menus Alphabetically
    By vandana43 in forum Applications
    Replies: 0
    Last Post: 03-12-2008, 10:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48