August 14th, 2003, 12:06 AM
STL and Visual C++
Hey guys, I'm new to this forum and I am hoping for some enlightenment. This is a very specific question about STL, any help will be appreciated. So here is my problem:
I need to sort a list in two different ways. So I am using STL's list<T>. The SGI documentation states that the sort() method belonging to this class can take 0 or 1 arguments. When no arguments are passed, the list is sorted using the object's less than operator; something like this will work: bool operator<(T,T). The other way to call sort() is to pass it a compare function. Herein lies the problem. With the gcc compiler one can pass a simple bool comp(T,T) but in VisualC++ it seems that if you give sort() some kind of parameter it tries to convert it to std::greater<T>. Even std::less<T> does not work. SGI's documentation says that a BinaryPredicate will work, Microsoft's documentation says it must be of class Traits. But like I said even std::less<T> or std::less_equal<T> or any of these will not work, only std::greater<T>. My solution was to define the greater operator to do a less than comparison. This cannot be the only way to do this. By the way, the sort() method from <algorithms> does not work on lists, that is why list has its own sort method.