Using STL's sort function for the 'list' class

Before I ask my question, I apologize for the crude outlook of my program; I tried to use the code tags on here, but, for some inconceivable reason, they would not work! Same goes for the other formatting tools on here. Even the text editor would not even generate a 'preview' of my post on clicking on the Preview button!! This was not the performance before though; I confess I've not been using this Forum a lot of recent.

Now to my question:
Does anyone know why the compiler keeps flagging my use of the STL's sort function in the program below?


#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;


struct spanishWord
{
string spaWord;
string engWord;
};


void loadMientos(ifstream& inp, list<spanishWord>& m);
void printMientos(list<spanishWord>& m);


int main()
{
ifstream infile;
ofstream outfile;

infile.open("mientos.txt");
outfile.open("mientos.txt");


list<spanishWord> mientos;
loadMientos(infile, mientos);
mientos.sort();
cout<<endl<<endl;
printMientos(mientos);


infile.close();
outfile.close();

// wait until user is ready before terminating program
system("PAUSE");
return 0;
}




void loadMientos (ifstream& inp, list<spanishWord>& m)
{
string sWord;
string eWord;
spanishWord obj;

while (!inp.eof())
{
cin>>sWord>>eWord;
obj.spaWord = sWord;
obj.engWord = eWord;
m.push_back(obj);
}
}



void printMientos (list<spanishWord>& m)
{
for (list<spanishWord>::iterator iter = m.begin(); iter != m.end(); iter++)
cout<<(*iter).spaWord<<" "<<(*iter).engWord<<endl;
}






Shown below are the error messages I got from the compiler:

C:\Dev-Cpp\include\c++\3.4.2\bits\list.tcc In member function `void std::list<_Tp, _Alloc>::merge(std::list<_Tp, _Alloc>&) [with _Tp = spanishWord, _Alloc = std::allocator<spanishWord>]':

256 C:\Dev-Cpp\include\c++\3.4.2\bits\list.tcc instantiated from `void std::list<_Tp, _Alloc>::sort() [with _Tp = spanishWord, _Alloc = std::allocator<spanishWord>]'

30 C:\Users\Kolly\Desktop\mientos1.cpp instantiated from here

221 C:\Dev-Cpp\include\c++\3.4.2\bits\list.tcc no match for 'operator<' in '(&__first2)->std::_List_iterator<_Tp>::operator* [with _Tp = spanishWord]() < (&__first1)->std::_List_iterator<_Tp>::operator* [with _Tp = spanishWord]()'


Last edited on
std::sort requires RandomAccessIterators. std::list provides only BidirectionalIterators, so std::sort cannot be used with std::list.

However std::list provides its own sorting member function: http://en.cppreference.com/w/cpp/container/list/sort
Hurray! My formatting tools are now working!

@MiiNiPaa:
Just to clarify, the sort() function I'm using is a member function of the std::list class. It appears the std::sort function you are referring to is a global (generic) function. Mine is similar to the example below, culled from one of the pages on this site (http://www.cplusplus.com/reference/list/list/sort/)


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
// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>

// comparison, not case sensitive.
bool compare_nocase (const std::string& first, const std::string& second)
{
  unsigned int i=0;
  while ( (i<first.length()) && (i<second.length()) )
  {
    if (tolower(first[i])<tolower(second[i])) return true;
    else if (tolower(first[i])>tolower(second[i])) return false;
    ++i;
  }
  return ( first.length() < second.length() );
}

int main ()
{
  std::list<std::string> mylist;
  std::list<std::string>::iterator it;
  mylist.push_back ("one");
  mylist.push_back ("two");
  mylist.push_back ("Three");

  mylist.sort();

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  mylist.sort(compare_nocase);

  std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}



How is this significantly different from the way I applied it for my case?

Last edited on
The sort function is not the problem; you have not defined a way to compare two spanishWord objects in your original example. The sort function will, unless you provide it with an alternative, try to use the less-than operator (<) to compare them, which you will want to define for your class if you want it to work.
Hmm..., let's see Zhuge; you may have a point there! Let me go and explore that angle.
Topic archived. No new replies allowed.