Re[2]: Moya sortirovka dlya std::list
От: DarkGray Россия http://blog.metatech.ru/post/ogni-razrabotki.aspx
Дата: 18.04.02 12:43
Оценка:
Здравствуйте Кодт, Вы писали:

Так в том то и дело, что это не работает. Так как вызывается operator() от класса greater, а не от твоего класса.

#include <iostream>
#include <list>
#include <algorithm>

#define for if(1) for


template<class T>
struct MyGreater : public std::greater<T>
{
  bool operator()(const T& x, const T& y) const
  {
    return x < y;
  }
};

template<class T>
struct MyGreater2 : public std::greater<T>
{
  bool operator()(const T& x, const T& y) const
  {
    return x > y;
  }
};


void main()
{
  typedef std::list<int> List;
  std::list<int> l;
  for (int i = 0; i < 10; i++)
  {
    l.push_back (i);
  }
  l.sort (MyGreater<int>());
  for (List::iterator it = l.begin(); it != l.end(); ++it)
  {
    std::cout << *it << "\n";
  }
  l.sort (MyGreater2<int>());
  for (List::iterator it = l.begin(); it != l.end(); ++it)
  {
    std::cout << *it << "\n";
  }
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.