清风竹林

ぷ雪飘绛梅映残红
   ぷ花舞霜飞映苍松
     ----- Do more,suffer less

C++中std::set<>集合中元素比较器(less<>)的设计

   一直没有总结过,每次设计都会出一些问题,要知道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(voidconst
    {
        
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()









posted on 2008-09-08 09:44 李现民 阅读(9976) 评论(0)  编辑 收藏 引用 所属分类: 语法试炼


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理