当前位置: 首页 > wzjs >正文

做网站公司长沙哪家好搜索词排行榜

做网站公司长沙哪家好,搜索词排行榜,wordpress 事件插件,推荐网站建设上篇文章我们讲解了哈希表的实现,这节尝试使用哈希表来封装unordered_set/map 1. unordered_set/map的框架 封装的过程实际上与set/map类似,在unordered_set/map层传递一个仿函数,用于取出key值 由于我们平常使用的都是unordered_set/map&…

上篇文章我们讲解了哈希表的实现,这节尝试使用哈希表来封装unordered_set/map

1. unordered_set/map的框架

封装的过程实际上与set/map类似,在unordered_set/map层传递一个仿函数,用于取出key值

由于我们平常使用的都是unordered_set/map,因此哈希函数应在unordered_set/map层作为参数传递给哈希表

// unordered_set.h
template<typename K>
struct HashFunc
{size_t operator()(const K& key){return (size_t)key;}
};template<>
struct HashFunc<string>
{size_t operator()(const string& s){size_t ans = 0;for (auto& e : s){ans *= 131;ans += e;}return ans;}
};namespace byh
{template<typename K, typename Hash = HashFunc<K>>class unordered_set{struct KeyOfSet{const K& operator()(const K& key){return key;}};public:bool insert(const K& key){return _table.insert(key);}private:hash_bucket::HashTable<K, K, KeyOfSet, Hash> _table;};
}// unordered_map.h
namespace byh
{template<typename K, typename V, typename Hash = HashFunc<K>>class unordered_map{struct KeyOfMap{const K& operator()(const pair<K, V>& kv){return kv.first;}};public:bool insert(const pair<K, V>& kv){return _table.insert(kv);}private:hash_bucket::HashTable<K, pair<K, V>, KeyOfMap, Hash> _table;};
}

2. 迭代器

在unordered_set/map这里,迭代器是对哈希节点指针的封装

遍历过程中,如果当前节点的next不为空,则直接迭代到下一节点;如果当前节点的next为空,则迭代到下一个不为空的桶

在这里插入图片描述

在这里插入图片描述

因此,迭代器中仅仅有节点的指针还不够,还要有哈希表;迭代器在构造时将节点的指针和哈希表的指针作为参数传递

但由于table在哈希表中是私有成员,迭代器类中不能直接访问table,这里有两种解决方法

  1. 将迭代器类声明为哈希表的友元类
  2. 在哈希表中使用内部类创建迭代器类,内部类天然就是外部类的友元类
// 友元的做法
template<typename K, typename T, typename KeyOfT, typename Hash>
class HashTable;template<typename K, typename T, typename Ref, typename Ptr, typename KeyOfT, typename Hash>
class __Iterator
{using Node = HashNode<T>;using Self = __Iterator<K, T, Ref, Ptr, KeyOfT, Hash>;
public:__Iterator(Node* node, HashTable<K, T, KeyOfT, Hash>* pht):_node(node),_pht(pht){}Self& operator++(){if (_node->_next) _node = _node->_next;else{KeyOfT kot;Hash hash;size_t pos = hash(kot(_node->_data)) % _pht->_table.size();int i = 0;for (i = pos + 1; i < _pht->_table.size(); i++){Node* cur = _pht->_table[i];if (cur){_node = cur;break;}}if(i == _pht->_table.size()) _node = nullptr;}return *this;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}private:Node* _node;HashTable<K, T, KeyOfT, Hash>* _pht;
};template<typename K, typename T, typename KeyOfT, typename Hash>
class HashTable
{friend __Iterator<K, T, T&, T*, KeyOfT, Hash>;
public:using Node = HashNode<T>;using iterator = __Iterator<K, T, T&, T*, KeyOfT, Hash>;// ......
}
// 内部类的做法
template<typename K, typename T, typename KeyOfT, typename Hash>
class HashTable
{template<typename Ref, typename Ptr>class __Iterator{using Node = HashNode<T>;using Self = __Iterator<Ref, Ptr>;public:__Iterator(Node* node, HashTable* pht):_node(node), _pht(pht){}Self& operator++(){if (_node->_next) _node = _node->_next;else{KeyOfT kot;Hash hash;size_t pos = hash(kot(_node->_data)) % _pht->_table.size();int i = 0;for (i = pos + 1; i < _pht->_table.size(); i++){Node* cur = _pht->_table[i];if (cur){_node = cur;break;}}if (i == _pht->_table.size()) _node = nullptr;}return *this;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}private:Node* _node;HashTable* _pht;};public:using Node = HashNode<T>;using iterator = __Iterator<T&, T*>;// ......
}

内部类做法的好处时,内部类能直接使用外部类的模板参数,不用向友元方式那样传递很多模板参数,同时内部类能直接使用外部类而不用传递模板参数

哈希表的迭代器一旦设计好,unordered_set/map层无非就是封装,这里不再叙述

3. const迭代器

const迭代器的整体思路没难度,但有些细节地方需要注意

在哈希表层和unordered_set/map层添加完const迭代器后

在这里插入图片描述

template<typename K, typename T, typename KeyOfT, typename Hash>
class HashTable
{template<typename Ref, typename Ptr>class __Iterator{using Node = HashNode<T>;using Self = __Iterator<Ref, Ptr>;public:__Iterator(Node* node, const HashTable* pht):_node(node), _pht(pht){}// ......private:Node* _node;const HashTable* _pht;};

关于unordered_map的operator[],其设计思路与set/map相同

unordered_set/map层的插入、删除、查找操作也同样是简单的封装

4. 测试

void Print(const unordered_set<int>& s)
{unordered_set<int>::const_iterator it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;
}void Test_unordered_set()
{vector<int> v = { 1, 4, 2, 3, 5, 9, 11 };unordered_set<int> s;for (auto& e : v) s.insert(e);cout << "const迭代器: ";Print(s);cout << s.find(4) << endl;cout << s.find(54) << endl;s.erase(4);cout << s.find(4) << endl;
}

在这里插入图片描述

void Test_unordered_map()
{unordered_map<string, int> m;vector<string> v = { "apple", "apple", "banana", "watermelon", "banana" };for (auto& e : v) m[e] ++;unordered_map<string, int>::iterator it = m.begin();while (it != m.end()){cout << it->first << " " << it->second << endl;++it;}cout << endl;cout << m.find("apple") << endl;cout << m.find("abdsaf") << endl;m.erase("apple");cout << m.find("apple") << endl;
}

在这里插入图片描述

http://www.dtcms.com/wzjs/31335.html

相关文章:

  • 网站建站那个好平台推广是做什么的
  • 网站建设与维护新的体会广州网络推广外包平台
  • 网站建设委托协议黑帽seo培训多少钱
  • 网站里的聊天怎么做的本地推广平台有哪些
  • 好看的中文网站设计小红书seo排名
  • 政府网站平台安全建设方案软件怎么推广
  • 唐山制作网站的搜索热门关键词
  • 网站开发团队排行榜网站查询入口
  • 网站icp备案 年检seo查询官方网站
  • wordpress文章目录分页搜索引擎优化实训心得
  • 如何做网站banner抖音关键词排名查询
  • 做旅游网站需要什么深圳白帽优化
  • 金融网站建设方案ppt模板下载潍坊seo关键词排名
  • 做网站是什么样的工作百度app安装免费下载
  • 什么网站发布公司销售潍坊关键词优化平台
  • 适合学生做网页练习的网站培训机构管理系统哪个好
  • 凤岗网站建设网站域名查询ip地址
  • 网站页面改版降权首页百度
  • 徐州网站建设培训班长沙seo步骤
  • 域名注册了 如何做网站百度怎么精准搜关键词
  • 什么网站可以免费做视频的软件下载北京网站优化服务商
  • 用PYTHON3 做网站百度竞价品牌广告
  • 山东企业网站备案网络营销专业代码
  • 网站服务内容怎么写域名网
  • 中山网站建设与设计提高工作效率的措施
  • 通州免费网站建设军事新闻 今日关注
  • 甘肃兰州旅游攻略seo实战技巧100例
  • 网站建设与维护的软件互联网宣传方式有哪些
  • 淅川微网站建设百度热门搜索排行榜
  • 购物网站备案费用什么是关键词广告