简单使用set/unordered_set (备忘)

在set/unordered_set  用自定义类型, 编译失败。 记录下。


在set 中使用自定义类型 mask, 需要提供 std::less<mask> 和等价的方式

set 构造函数的原型
set (const _Compare &__comp, const allocator_type &__a=allocator_type())

如果是自定义变量需要提供 Compare, compare 默认是用std::less. std::less 的默认实现是用 对应类型的 operator < 。

如果同时提供 bool mask::operator<(const mask&) const  和 bool operator<(const mask&, const mask&) 编译器就会报错

自定义类型mask 
提供 operator <, std::less 的默认实现
struct mask {
  string str;
  bool operator < (const mask &r) const {
    return str < r.str;
  }
};

特化std::less<mask>
namespace std {
  template <>
  struct less<mask> {
    bool operator () (const mask &l, const mask &r) {
      return l.str < r.str;
    }
  };
}

std::less 默认实现
bool operator < (const mask &l, const mask &r) {
  return l.str < r.str;
}

初始化set<mast> 指定 compare : set<mask, mask_less>
struct mask_less : public std::binary_function <mask, mask, bool> {
  bool operator () (const mask &l, const mask &r) const {
    return l.str < r.str;
  }
};

/usr/include/c++/4.7/bits/stl_function.h:237:22: error: ambiguous overload for ‘operator<’ in ‘__x < __y’
/usr/include/c++/4.7/bits/stl_function.h:237:22: note: candidates are:
test_set.cpp:9:8: note: bool mask::operator<(const mask&) const
test_set.cpp:23:6: note: bool operator<(const mask&, const mask&)


unordered_set & mask 

unordered_set 和 std::hash, std::equal_to 的原型
template<
    class Key,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator<Key>
> class unordered_set;

template<typename _Tp>struct std::hash< _Tp >  
    template<typename _Tp>
    struct hash : public __hash_base<size_t, _Tp>
    {
      static_assert(sizeof(_Tp) < 0,
                    "std::hash is not specialized for this type");
      size_t operator()(const _Tp&) const noexcept;
    };

template<typename _Tp>struct std::equal_to< _Tp >
  template<typename _Tp>
    struct equal_to : public binary_function<_Tp, _Tp, bool>
    {
      bool operator()(const _Tp& __x, const _Tp& __y) const
      { return __x == __y; }
    };

unordered_set 和mask 类一起使用, mask 类型 需要提供std::hash<mask> , std::equal_to<mask>.  

struct mask {
  string str;
  size_t hash_value () const {
    return boost::hash_range (str.begin (), str.end ());
  }
};

namespace std {
  template <>
  struct hash<mask> {
    size_t operator () (const mask &m) const {
      return boost::hash_range (str.begin (), str.end ());
    }
  };

  template <>
  struct equal_to<mask> : public binary_function<mask, mask, bool> {
    bool operator () (const mask &l, const mask &r) const {
      return l.hash_value () == r.hash_value ();
    }
  };
}


boost 对C++内置的类型 (int, char 等) 的 hash 默认支持。 借用boost::hasn_range<char> 实现mask 的hash计算





unordered_set和unordered_map是C++标准库中的容器,用于存储和管理不重复的元素集合和键值对集合。 unordered_set是一个无序的集合容器,其中的元素是唯一的且无序的。它基于哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间O(1)。使用unordered_set可以快速判断一个元素是否存在于集合中。 unordered_map是一个无序的键值对容器,其中的键是唯一的且无序的。它也基于哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间O(1)。使用unordered_map可以根据键快速查找对应的值。 使用unordered_set和unordered_map时,需要包含头文件<unordered_set>和<unordered_map>。以下是它们的基本用法: 1. 创建容器: unordered_set<int> mySet; // 创建一个空的unordered_set unordered_map<string, int> myMap; // 创建一个空的unordered_map 2. 插入元素或键值对: mySet.insert(10); // 插入元素10到unordered_set中 myMap["apple"] = 5; // 插入键值对("apple", 5)到unordered_map中 3. 删除元素或键值对: mySet.erase(10); // 从unordered_set中删除元素10 myMap.erase("apple"); // 从unordered_map中删除键为"apple"的键值对 4. 查找元素或键值对: auto it = mySet.find(10); // 在unordered_set中查找元素10,返回迭代器 if (it != mySet.end()) { // 找到了元素 } auto it = myMap.find("apple"); // 在unordered_map中查找键为"apple"的键值对,返回迭代器 if (it != myMap.end()) { // 找到了键值对 int value = it->second; // 获取对应的值 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值