码迷,mamicode.com
首页 > 其他好文 > 详细

std::map, struct pointer 为key

时间:2014-11-26 11:06:25      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   sp   for   on   cti   bs   ef   

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

  1. struct Cell{
  2. int x;
  3. int y;
  4. Cell(int _x,int _y):x(_x),y(_y){}
  5. };
  6. struct cmp{
  7. bool operator()(const Cell* const c1, const Cell * const c2) {
  8. if(c1->x == c2->x && c1->y == c2->y) return false;
  9. else return true;
  10. }
  11. };
  12. int main(){
  13. map<Cell *,int,cmp> mp; // 根据cmp定义的比较函数来比较,如何不相等,则返回true,没有定义<和>
  14. Cell *c1 = new Cell(1,2);
  15. Cell *c2 = new Cell(3,4);
  16. mp[c1] = 1;
  17. mp[c2] = 1;
  18. Cell *c3 = new Cell(1,2);
  19. // mp[c3]=1;
  20. cout<<mp.size()<<endl;
  21. if(mp.find(c3) == mp.end()){
  22. cout<<"can not find it!"<<endl;
  23. } else {
  24. cout<<"yes,it exists"<<endl;
  25. }
  26. return 0;
  27. }

默认的key_comp, 

key_compare key_comp() const;
Returns a copy of the comparison object used by the container to compare keys.
By default, this is a less object, which returns the same as operator<.
his object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the element keys, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.

Two keys are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).


std::map, struct pointer 为key

标签:http   io   ar   sp   for   on   cti   bs   ef   

原文地址:http://www.cnblogs.com/purejade/p/4122531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!