STL set的使用 less function override < operator

一直没有总结过,每次设计都会出一些问题,要知道STL的报错可不是那么容易看懂的。

   假定需要设计的类为Country,各country之类比较的依据是人口population,分析如下:

   情形一:如果类Country类是你本人设计的,即你拥有对Country类的修改权,那么在类中重载operator<就可以了,需要注意的就是必须将其设计为const成员函数,如下:

class Country

{

public:

    explicit Country(int population)

     {

         m_nPopulation=population;

     }

    void print()

     {

         std::cout<<m_nPopulation<<std::endl;

     }

    //注:operator<必须是const成员函数

    bool operator<(const Country& b)const

     {

        return m_nPopulation<b.m_nPopulation;

     }

private:

    int m_nPopulation;

};

int main(int argc, char* argv[])

{

     Country countryArray[]={Country(43), Country(54),Country(85),Country(95),Country(12),Country(57),Country(124),Country(78),

         Country(45), Country(56),Country(87),Country(457),Country(567),Country(123),Country(456),Country(237),

         Country(43), Country(784),Country(728),Country(76),Country(467),Country(83),Country(723),Country(86)};

     std::set<Country> countrySet;

    int nSize=sizeof countryArray/ sizeof countryArray[0];

    for (int i=0; i<nSize; ++i)

     {

         countrySet.insert(countryArray[i]);

     }

    for (std::set<Country>::iterator iter=countrySet.begin(); countrySet.end()!=iter; ++iter)

     {

         iter->print();

     }

    //Sleep(int(2e9));

     system("pause");

    return 0;

}

   情形二:Country为不是你设计的,即类中没有已存在的operator<比较器,同时你也无法对其进行修改,那么你现在需要做的是在外部设计一个比较器,确切的说,是一个函数对象(或函数,但函数无法内联),如下:

class Country

{

public:

    explicit Country(int population)

     {

         m_nPopulation=population;

     }

    void print()

     {

         std::cout<<m_nPopulation<<std::endl;

     }

    //比上一个设计需要新加入一个函数

    int GetPopulation(void) const

     {

        return m_nPopulation;

     }

private:

    int m_nPopulation;

};

int main(int argc, char* argv[])

{

     Country countryArray[]={Country(43), Country(54),Country(85),Country(95),Country(12),Country(57),Country(124),Country(78),

         Country(45), Country(56),Country(87),Country(457),Country(567),Country(123),Country(456),Country(237),

         Country(43), Country(784),Country(728),Country(76),Country(467),Country(83),Country(723),Country(86)};

    //设计函数对象

    struct country_less:public std::binary_function<Country, Country, bool>

     {

        //这个倒不必须成为const成员函数,呵呵

        bool operator()(const Country& a, const Country& b)const

         {

            return a.GetPopulation()<b.GetPopulation();

         }

     };

    //std::set的定义就要复杂一些了

     std::set<Country, country_less> countrySet;

    int nSize=sizeof countryArray/ sizeof countryArray[0];

    for (int i=0; i<nSize; ++i)

     {

         countrySet.insert(countryArray[i]);

     }

    //同样,迭代器的定义也要复杂一些

    for (std::set<Country, country_less>::iterator iter=countrySet.begin(); countrySet.end()!=iter; ++iter)

     {

         iter->print();

     }

    //Sleep(int(2e9));

     system("pause");

    return 0;

}

   这两种情况的作用就是std::less<>谓词,因此同样适用于std::sort()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值