尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
C++ LFU嵌LRU递进缓存
背景怎么样选出最受欢迎的水果中最近购买的品种的价格LRU类#include list #include map #include unordered_map templatetypename K, typename V class LRUMap { public: using ListType std::liststd::pairK, V; using CacheMap std::mapK, typename ListType::iterator; using CacheMapIter typename CacheMap::iterator; LRUMap(int cap 100) : m_capacity(cap) { } virtual ~LRUMap() { } CacheMapIter find(const K key) { return m_cache.find(key); } bool isValid(const CacheMapIter iter) const { return iter ! m_cache.end(); } V get(const K key) { return get(find(key)); } /** * brief 缓存查询 * param it */ V get(const CacheMapIter it) { if (it m_cache.end()) return {}; m_list.splice(m_list.begin(), m_list, it-second); return it-second-second; } /** * brief 缓存新增 * param key * param value */ void put(const K key, V value) { if (m_list.size() m_capacity) { // 先删除最后一个 auto last m_list.end(); --last; m_cache.erase(last-first); m_list.pop_back(); } m_list.emplace_front(key, value); m_cache.try_emplace(key, m_list.begin()); } /** * brief 热度更新。数据变更 * param it */ void update(const CacheMapIter it) { if (it m_cache.end()) return; if (m_list.size() 2) return; m_list.splice(m_list.begin(), m_list, it-second); it-second m_list.begin(); } /** * brief 热度更新。数据不变 * param it * param value */ void update(const CacheMapIter it, V value) { if (it m_cache.end()) return; if (m_list.empty()) return; if (m_list.size() 2) { m_list.begin()-second value; return; } m_list.splice(m_list.begin(), m_list, it-second); it-second m_list.begin(); it-second-second value; } private: int m_capacity; ListType m_list; std::mapK, typename ListType::iterator m_cache; };LRU键类class Key { public: Key(std::string type, std::string color):m_type(type), m_color(color){} std::string type() const { return m_type; } std::string color() const { return m_color; } private: bool operator(const Key other) const { return if(m_type other.type()) return m_color other.color(); return m_type other.type();} bool operator(const Key other) const { return m_type other.type() m_color other.color(); } private: std::string m_type; std::string m_color; };LRU扩展类class LRUCache : public LRUMapKey, float { public: LFUCache(int cap) : LRUMapKey, float(cap) , m_freq(0) { } int freq() const { return m_freq; } void setFreq(int freq) { m_freq freq; } private: int m_freq; };LFU类// LRU最近使用缓存例如青苹果和红苹果都是苹果最近出现的颜色被缓存。 // LFU出现最多缓存例如香蕉和苹果都是水果应该被分类出现次数多的水果被缓存。 // LFU嵌LRU类 class LFUCache { public: using Iter std::unordered_mapstd::string, LRUCache::iterator; LFUCache(int cap) : m_capacity(cap) { } LRUCache insert(const std::string content) { auto findIter m_fruitLRUMap.find(content); if (findIter m_fruitLRUMap.end()) { if (m_fruitLRUMap.size() m_capacity) shrink_to_fit_one(); findIter m_fruitLRUMap.try_emplace(content, 12).first; } updateFreq(findIter); return findIter-second; } private: /* * * 删除一个低频次的LRU缓存 */ void shrink_to_fit_one() { if (m_fruitFreqMap.empty()) return; auto lowFreqIter m_fruitFreqMap.begin(); if (lowFreqIter-second.empty()) return; auto removeIter lowFreqIter-second.begin(); m_fruitLRUMap.erase(*removeIter); // 更新LRU缓存 if (lowFreqIter-second.size() 2) { m_fruitFreqMap.erase(lowFreqIter); return; } lowFreqIter-second.erase(removeIter); // 删除低频次的一个 } void updateFreq(Iter iter) { // 从旧的频次中删除 auto oldFreq iter-second.freq(); auto oldFreqFindIter m_fruitFreqMap.find(oldFreq); if (oldFreqFindIter ! m_fruitFreqMap.end()) { if (oldFreqFindIter-second.size() 2) m_fruitFreqMap.erase(oldFreq); // 只剩一个整体删除 else oldFreqFindIter-second.erase(iter-first); // 局部删除 } // 新的频次添加 auto newFreq oldFreq 1; auto newFreqFindIter m_fruitFreqMap.find(newFreq); if (newFreqFindIter m_fruitFreqMap.end()) m_fruitFreqMap.try_emplace(newFreq, std::unordered_setstd::string{ iter-first }); else newFreqFindIter-second.insert(iter-first); iter-second.setFreq(newFreq); } private: std::unordered_mapstd::string, LRUCache m_fruitLRUMap; // 每种水果最近出现的品种 std::mapint, std::unordered_setstd::string m_fruitFreqMap; // 经常出现的水果 int m_capacity; };使用示例一// 水果最近有被购买 bool testPriceChange(const Key key, float price) { LRUMapstd::string, Key lastFruitMap; auto findCacheIter lastFruitMap.find(key.type()); if (!lastFruitMap.isValid(findCacheIter)) return true; if (findCacheIter-second-second ! price) return true; return false; } Key key(苹果红色); float price 10.f; auto isChange testPriceChange(key, price); std::cout Price is change isChange std::endl;使用示例二// 统计经常被购买水果价格 float testPrice(const Key key) { LFUCache fruitCache; auto lruCache fruitCache.insert(key.type()); auto cacheIter lruCache.find(key); bool hasCache lruCache.isValid(cacheIter); float price; if (hasCache) { // 最近有被购买 price cacheIter-second-second; lruCache.update(cacheIter); } else { price 10; // 请输入价格 lruCache.put(key, price); } return price; } Key key(苹果红色); auto price testPrice(key); std::cout Price is price std::endl;参考https://www.geeksforgeeks.org/lru-cache-implementation/https://leetcode.cn/problems/lru-cache/https://www.geeksforgeeks.org/least-frequently-used-lfu-cache-implementation/https://leetcode.cn/problems/lfu-cache/创作不易小小的支持一下吧
RELATED

相关推荐

HarmonyOS DevEco Studio的使用练习题

HarmonyOS DevEco Studio的使用练习题

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/11/2 6:18:11
JavaScript - 比较运算符:等于与全等的核心区别

JavaScript - 比较运算符:等于与全等的核心区别

👋 大家好,欢迎来到我的技术博客! 📚 在这里,我会分享学习笔记、实战经验与技术思考,力求用简单的方式讲清楚复杂的问题。 🎯 本文将围绕JavaScript这个话题展开,希望能为你带来一些…

📅 2026/11/2 6:18:11
FreeToken 快速上手:用 ft serve / ft shell / ft launch 在本地跑起 MoE 大模型服务

FreeToken 快速上手:用 ft serve / ft shell / ft launch 在本地跑起 MoE 大模型服务

FreeToken 快速上手:用 ft serve / ft shell / ft launch 在本地跑起 MoE 大模型服务 【免费下载链接】FreeToken FreeToken brings datacenter-scale model serving to your desktop. Run massive models locally, fast and efficiently. 项目地址: https://gitc…

📅 2026/11/2 6:18:11
MORE NEWS

更多资讯

📰

微电网中光伏与空调负荷的优化控制策略

1. 项目背景与核心价值去年夏天参与某工业园区微电网项目时,我亲历了这样一个场景:当光伏发电达到峰值时,配电变压器却因空调集中启动导致过载跳闸。这个典型矛盾促使我开始深入研究如何协调可再生能源波动与空调负荷的关系。这项研究本质上要…

📰

ERP库存上下限管理:设置技巧与预警优化

1. 库存上下限管理的核心价值与业务场景库存上下限设置是ERP系统中最为基础却又极其关键的库存管控功能。在管家婆云辉煌ERP中,这个功能模块直接关系到企业的库存周转效率和资金占用情况。根据我多年实施ERP系统的经验,合理设置库存上下限可以帮助企业实…

📰

基于MCP协议构建跨平台AI指挥中心的技术实践

1. 项目概述:构建跨平台AI指挥中心去年在为一个跨国团队做技术支持时,我遇到了一个棘手问题:团队成员分散在Slack和Telegram两个平台,关键消息经常被遗漏。这促使我开始研究如何通过MCP协议打造一个真正的跨平台AI指挥中心。MCP&a…

📰

C++ STL全排列:next/prev_permutation避坑

刷算法题也好&#xff0c;写实际业务代码也好&#xff0c;只要碰到“把一堆元素的所有排列都列出来”这种需求&#xff0c;C 选手脑子里第一时间蹦出来的基本就是next_permutation和prev_permutation这两个函数。它们藏在<algorithm>头文件里&#xff0c;用起来一行就能推…

📰

档案管理系统设计与实现:从架构到安全

1. 项目概述"记录档案1"这个项目名称看似简单&#xff0c;却蕴含着丰富的信息管理内涵。作为一名从业多年的数据管理专家&#xff0c;我理解这类项目通常涉及信息收集、分类存储、快速检索等核心功能。在实际工作中&#xff0c;一个高效的档案管理系统能够显著提升工…

📰

VeRL框架与PPO算法在机器人控制中的应用实践

1. VeRL框架与PPO算法背景解析在深度强化学习领域&#xff0c;VeRL(Verifiable Reinforcement Learning)框架因其独特的可验证性设计而备受关注。这个框架特别适合需要高可靠性的应用场景&#xff0c;比如机械臂控制、足式机器人运动规划等。我最早接触VeRL是在开发仓储自动化项…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬