Chinaunix首页 | 论坛 | 博客
  • 博客访问: 832338
  • 博文数量: 155
  • 博客积分: 4004
  • 博客等级: 中校
  • 技术积分: 2070
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-19 11:37
文章分类

全部博文(155)

文章存档

2014年(3)

2013年(9)

2012年(28)

2011年(20)

2010年(29)

2009年(66)

我的朋友

分类: C/C++

2013-08-24 15:29:27

1. 使用vector进行排序插入
    如果我们有个vector容器,如std::vector vMyVec,里面现在已经有1,3,5,7, 四个元素,现在需要往1,3之间插入2这个元素,那怎么办呢,更或者vector里面是个自定义的类型,需要做这种插入操作,那可以采用下面的方法

点击(此处)折叠或打开

  1. #include <vector>
  2. #include <algorithm>
  3. class CEntity
  4. {
  5. public:
  6.     CEntity( int nValue=0 ):m_nValue(nValue)
  7.     {

  8.     }
  9.     int m_nValue;
  10. };

  11. class CLess
  12. {
  13. public:
  14.     bool operator()( const CEntity& lh, const CEntity& rh )
  15.     {
  16.         //这里还可以进行一些自定义的判断方式

  17.         return lh.m_nValue < rh.m_nValue;
  18.     }
  19. };
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.          std::vector<CEntity> vEntity1;
  23.     std::vector<CEntity> vEntity2;
  24.     for ( int n=0; n<10; n++ )
  25.     {
  26.         nValue = rand()%10;
  27.         vEntity1.insert(std::upper_bound(vEntity1.begin(),vEntity1.end(),CEntity(nValue),CLess()), CEntity(nValue) );
  28.         vEntity2.push_back( CEntity(nValue) );
  29.     }
  30. }
2. std::upper_bound 的通用class Predicate
当我们要对std容器进行自定义排序插入操作,需要用到std::upper_bound和std::lower_bound,这两个函数都需要提供自定义的排序类,这里讨论的就是实现这个可通用的实现排序类:

点击(此处)折叠或打开

  1. template<typename _TKey, typename _TData>
  2. class CKeyData
  3. {
  4. public:
  5.     CKeyData(const _TKey& k, const _TData& d):m_Key(k)
  6.         , m_Data(d)
  7.     {

  8.     }
  9.     ~CKeyData()
  10.     {

  11.     }
  12.     _TKey m_Key;
  13.     const _TData& m_Data;
  14. };

  15. template<typename _TKey, typename _TData>
  16. class CKeyUpSort
  17. {    
  18. public:
  19.     bool operator()( const CKeyData<_TKey,_TData>& lh, const CKeyData<_TKey,_TData>& rh )
  20.     {
  21.         return lh.m_Key > rh.m_Key;
  22.     }
  23. };

  24. template<typename _TKey, typename _TData>
  25. class CKeyLowSort
  26. {    
  27. public:
  28.     bool operator()( const CKeyData<_TKey,_TData>& lh, const CKeyData<_TKey,_TData>& rh )
  29.     {
  30.         return lh.m_Key < rh.m_Key;
  31.     }
  32. };

  33. class CMyData
  34. {
  35. public:
  36.     CMyData( int nId ):m_nId(nId)
  37.     {
  38.         
  39.     }
  40.     int m_nId;
  41. };

  42. int main(int argc, char *argv[])
  43. {
  44.     //假设现在有一个无序的实体列表
  45.     typedef std::list<CMyData> MyList;
  46.     MyList listMyData;
  47.     for ( int n=0; n<10; n++ )
  48.     {
  49.         listMyData.push_back(CMyData(rand()%10));
  50.     }

  51.     typedef CKeyData<int,CMyData> MyKeyData;
  52.     typedef CKeyUpSort<int,CMyData> MySort;
  53.     std::list<MySort> listMy;
  54.     MyList::iterator itr = listMyData.begin();
  55.     MyList::iterator itrE = listMyData.end();
  56.     for ( ; itr!=itrE; ++itr )
  57.     {
  58.         CMyData& data = *itr;
  59.         //以data.m_nId为键值,进行降序插入
  60.         MyKeyData keydata(data.m_nId,data);
  61.         listMy.insert( std::upper_bound(listMy.begin(),listMy.end(),keydata,MySort()), keydata );
  62.     }
  63.     
  64.     return 0;
  65. }


阅读(8654) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~