#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
class Integer
{
public:
  Integer(void):m_nValue(100)
  {
    cout<<"Integer::Integer()invoked"<<endl;
  }
  Integer(int nValue):m_nValue(nValue){}
  Integer(const Integer& intOther):m_nValue(intOther.m_nValue)
  {
    cout<<"Integer(Integer)"<<endl;
  }
  Integer& operator=(const Integer& intOther)
  {
    cout<<"Integer::operator"<<endl;
    if(&intOther!=this)
m_nValue=intOther.m_nValue;
    return*this;
  }
/*  bool operator<(const Integer&intRight)const
  {
    return m_nValue<intRight.m_nValue;
  }*/
  int m_nValue;
};
class IntegerComparator
{
public:
  bool operator()(const Integer& intLeft,const Integer&intRight)
  {
    return intLeft.m_nValue<intRight.m_nValue;
  }
};
int main()
{
  vector<Integer>vi1(3);
  vector<Integer>vi2;
  vi2.push_back(Integer(2));
  vi2[0]=Integer(3);
  vi2.resize(4);
  vi2[1]=Integer(12);
  vi2[2]=Integer(2);
  vi2[3]=Integer(58);

  sort(vi2.begin(),vi2.end(),IntegerComparator());
  
  return 0;
}

解决方案 »

  1.   

    类似:
    int compare( const void *arg1, const void *arg2 );
    qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );
      

  2.   

    sort内部来决定调用哪两个做参数来比较
      

  3.   

    “IntegerComparator()”
    这个()表示是个函数,不需要参数。
      

  4.   

    IntegerComparator()这是调用构造函数啊,构造匿名对象.
    sort内部才会用到bool IntegerComparator::operator()(const Integer& intLeft,const Integer&intRight)
      

  5.   


    什么啊?这是有参构造函数好不好!!!下面的例子不是也用参数的吗[code]
    &#35;include <iostream>
    using namespace std;
    class Time{
        int hour;
        int minute;
        int second;
    public:
        Time( int h=0, int m=0, int s=0 )
        {
            operator()( h, m, s );
        }
        //版本0,返回时间表示的秒数
        int operator()()
        {
            return hour*3600+minute*60+second;
        }
        //版本1,设置为整点
        void operator()( int h )
        {
            operator()( h, 0, 0 );
        }
        //版本2,设置整小时和分钟
        void operator()( int h, int m )
        {
            operator()( h, m, 0 );
        }
        //版本3,设置时分秒
        void operator()( int h, int m, int s )
        {
            hour = h;
            minute = m;
            second = s;
        }
        friend ostream& operator<<( ostream& os, const Time& ct )
        {
            os << ct.hour << ';:';;
            if( ct.minute<10 )
                os << ';0';;
            os << ct.minute << ';:';;
            if( ct.second<10 )
                os << ';0';;
            os << ct.second;
            return os;
        }
    };
    int main()
    {
        Time t;
        cout << t << endl;
        t( 9 );//调用版本1
        cout << t << endl;
        t( 7, 30 );//调用版本2
        cout << t << endl;
        t( 0, 10, 20 );//调用版本3
        cout << t << endl;
        cout << t() << endl;//调用版本0
        return 0;
    }
    [/code]
      

  6.   


    class IntegerComparator
    {
    public:
          bool operator()(const Integer& intLeft,const Integer&intRight)
          {
                return intLeft.m_nValue<intRight.m_nValue;
          }
    };
    构造函数都没写,哪来的带参数构造?
      

  7.   

    你还没弄清楚 构造函数和operator()的区别。
    在Time类的成员函数加上断点,单步调试,看看哪一步都执行了那个函数。