Questions on using the STL list sort method

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster

Questions on using the STL list sort method

 
0
  #1
28 Days Ago
I am very new to using the STL list library. From what I have been reading here and other places on the web is that I have to create a compare function to use within the sort member function of list. The reason being that the list node's contain structures. I have to sort the list by a few different variables. Below is the class that controls my list. I am getting two errors.

Error 1) error C3867: 'Standings::compareForSort': function call missing argument list; use '&Standings::compareForSort' to create a pointer to member

Error 2) error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments

Any help here would be appreciated.

  1. #ifndef STANDINGS_H
  2. #define STANDINGS_H
  3.  
  4. #include <iostream>
  5. #include <list>
  6. #include <fstream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. #define NAMELENGTH 20
  12.  
  13. struct Records
  14. {
  15. int totalWin;
  16. int totalLose;
  17. double winPercentage;
  18. double gamesBehind;
  19. int streakWin;
  20. int streakLose;
  21. int streakLength;
  22. int homeWin;
  23. int homeLose;
  24. int awayWin;
  25. int awayLose;
  26. int interWin;
  27. int interLose;
  28. int division;
  29. char teamName[NAMELENGTH];
  30. char streakType;
  31.  
  32. };
  33.  
  34.  
  35. class Standings
  36. {
  37. private:
  38. list<Records> recordsList;
  39. friend class Team;
  40. public:
  41. Standings();
  42. ~Standings(){};
  43. void loadList();
  44. void saveList();
  45. void printStandings();
  46. bool compareForSort (Records&, Records&);
  47.  
  48. };
  49.  
  50. #endif
  51.  
  52. #include "standings.h"
  53.  
  54. Standings::Standings()
  55. {
  56.  
  57. }
  58.  
  59.  
  60. void Standings::loadList(){
  61. //Create a variable of ifstream type names loadFile
  62. ifstream loadFile;
  63. //Open standings txt file
  64. loadFile.open("standings.txt");
  65. //Temporary object of struct type Record
  66. Records r;
  67.  
  68.  
  69. //Loop to take from file put into class
  70. //and than put class into list
  71. while(!loadFile.eof()){
  72. loadFile >> r.teamName
  73. >> r.totalWin
  74. >> r.totalLose
  75. >> r.winPercentage
  76. >> r.gamesBehind
  77. >> r.streakWin
  78. >> r.streakLose
  79. >> r.streakType
  80. >> r.streakLength
  81. >> r.homeWin
  82. >> r.homeLose
  83. >> r.awayWin
  84. >> r.awayLose
  85. >> r.interWin
  86. >> r.interLose
  87. >> r.division;
  88.  
  89. //Checks to make sure its not the end of file.
  90. //If its not the end of the file it inserts
  91. //the node at the end of the list
  92. if(!loadFile.eof())
  93. recordsList.push_back(r);
  94. }
  95. //closing the text file
  96. loadFile.close();
  97. }
  98.  
  99. void Standings::saveList()
  100. {
  101. //Sorts the List before saving to txt
  102. recordsList.sort(compareForSort);
  103. //Create a variable of ofstream type names saveFile
  104. ofstream saveFile;
  105. //Open standings txt file
  106. saveFile.open("standings.txt");
  107. //Create an iterator to keep place of the nodes saved
  108. list<Records>::iterator i;
  109.  
  110. //Loop through each node and save each member to that node
  111. for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  112. saveFile << right << setw(13) << i->teamName << setw(4) << i->totalWin
  113. << setw(4) << i->totalLose << setw(7) << setprecision(3) << i->winPercentage
  114. << setw(6) << i->gamesBehind << setw(2) << i->streakWin
  115. << setw(2) << i->streakLose << setw(2) << i->streakType
  116. << setw(2) << i->streakLength << setw(3)<< i->homeWin
  117. << setw(3) << i->homeLose << setw(3) << i->awayWin
  118. << setw(3) << i->awayLose << setw(3)<< i->interWin
  119. << setw(3) << i->interLose << setw(2) << i->division
  120. << endl;
  121. }
  122. //Flush and Close txt file
  123. saveFile.flush();
  124. saveFile.close();
  125.  
  126. }
  127.  
  128. void Standings::printStandings()
  129. {
  130. list<Records>::iterator i;
  131.  
  132. //Looping through each node and printing each variable of each node
  133. for ( i=recordsList.begin(); i != recordsList.end(); ++i )
  134. {
  135. cout << right << setw(13) << i->teamName << setw(4) << i->totalWin
  136. << setw(4) << i->totalLose << right << setw(7) << i->winPercentage
  137. << right << setw(6) << i->gamesBehind << right << setw(2) << i->streakWin
  138. << right << setw(2) << i->streakLose << right << setw(2) << i->streakType
  139. << right << setw(2) << i->streakLength <<right << setw(3)<< i->homeWin
  140. << right << setw(3) << i->homeLose << right << setw(3) << i->awayWin
  141. << right << setw(3) << i->awayLose << right << setw(3)<< i->interWin
  142. << right << setw(3) << i->interLose << right << setw(2) << i->division
  143. << endl;
  144. }
  145. }
  146.  
  147.  
  148. bool Standings::compareForSort(Records& node1, Records& node2)
  149. {
  150. list<Records>::iterator i;
  151. for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  152. if((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division))
  153. return true;
  154. else
  155. return false;
  156.  
  157. }
  158.  
  159. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,261
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
27 Days Ago
You cannot use std::sort with list (if that is what you intended to do)

list has its own sort. Here is an example : http://www.cplusplus.com/reference/stl/list/sort/
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster
 
0
  #3
27 Days Ago
Line 102 is where I call sort. recordsList is the name of my list, and compareForSort is the name of the function I wrote Though I am sure I need help with that function as well. Maybe I am trying to use std::sort but I thought recordsList.sort(compareForSort) was calling the list specific sort. Btw cplusplus is one of the places I did read about creating the compare function.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster
 
-1
  #4
27 Days Ago
shameless bump. Sorry but I could really use some help on this subject. Thank you ahead of time for any help received.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,261
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
27 Days Ago
  1. recordsList.sort(compareForSort);

Try this :
  1. recordsList.sort(&Standings::compareForSort);
Last edited by firstPerson; 27 Days Ago at 6:39 pm.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 13
Reputation: Zjarek is an unknown quantity at this point 
Solved Threads: 3
Zjarek Zjarek is offline Offline
Newbie Poster
 
0
  #6
27 Days Ago
edit: sorry, my mistake, I mixed list with sort.
Last edited by Zjarek; 27 Days Ago at 8:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster
 
0
  #7
27 Days Ago
Ok now I am just getting

xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments

I assume it is talking about the binary predicate function, but everywhere I read the binary predicate in other peoples example has two arguments.
Does it have to do with the fact that The nodes in the list are of struct Records while the list is being created in the standings class? Though the variables in Records are public so I dont see why that would be an issue. Here is the predicate function I have currently
  1. bool Standings::compareForSort(const Records& node1, const Records& node2)
  2. {
  3. return ((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division));
  4.  
  5.  
  6. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster
 
0
  #8
27 Days Ago
After doing more reading I found that some people use an overloaded < operator. So I attempted this and still getting loads of errors.

error C2676: binary '<' : 'Records' does not define this operator or a conversion to a type acceptable to the predefined operator

Here is my overloaded <
  1. bool Standings::operator <(const Records& node)
  2. {
  3. list<Records>::iterator i;
  4. //for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  5. if ((i->totalWin > node.totalWin) && (i->winPercentage > node.winPercentage) && (i->division < node.division))
  6. return true;
  7. else
  8. return false;
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Afupi is an unknown quantity at this point 
Solved Threads: 0
Afupi Afupi is offline Offline
Newbie Poster
 
0
  #9
27 Days Ago
I am going to continue putting the things I try because I dont want anyone to think I am not trying for myself. So I tried putting the overloaded < operator within my struct Records. No errors. The program is running fine. Except when it is time to sort its not sorting anything. So I guess it could be the logic or syntax within the operator
EDIT: Is there another way to sort a struct list by multiple variable values?

  1. bool operator <(const Records& node)
  2. {
  3. return ((totalWin > node.totalWin) && (winPercentage > node.winPercentage) && (division < node.division));
  4. }
Last edited by Afupi; 27 Days Ago at 1:06 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,261
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #10
27 Days Ago
>>xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments

Which line is this from?
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC