[英]FIFO Map in c++
I need to store a number of key/value pairs and access them again referenced by key - not necessarily in a map, although this seems natural. 我需要存储一些键/值对并再次通过键引用它们 - 不一定在地图中,尽管这看起来很自然。 Additionally, if the map exceeds a certain size, I need to delete the oldest pairs.
此外,如果地图超过一定的大小,我需要删除最旧的对。
Is there a way to implement this using a map or a similar structure somehow combining a map and a queue in C++11? 有没有办法使用地图或类似的结构以某种方式在C ++ 11中组合地图和队列来实现它?
UPDATE: I wanted to this with a std::unsorted_map
. 更新:我想用
std::unsorted_map
。 Unfortunately I'm heavily missing std::map
functions which would help. 不幸的是,我非常缺少
std::map
函数,这会有所帮助。 The unordered list seems neither to support rbegin()
nor does its iterator support the --
operator, so that I can't use end()
either. 无序列表似乎既不支持
rbegin()
也不支持迭代器--
运算符,所以我也不能使用end()
。
Is there a better way than iterating through a loop to size()-1
? 有没有比循环遍历
size()-1
更好的方法?
There's no unique solution for this problem, the simplest one would be to use an auxiliary queue for storing the keys in order of insertion. 对于这个问题没有唯一的解决方案,最简单的方法是使用辅助队列按插入顺序存储密钥。
map<string, string> m_myMap;
queue<string> m_myQueue;
void insert(const string& key, const string& value) {
m_myMap.insert(make_pair(key, value));
m_myQueue.push(key);
}
void deleteOldOnes() {
while (m_myQueue.size() > MAX_SIZE) {
m_myMap.erase(m_myQueue.front());
m_myQueue.pop();
}
}
You keep using the map for accessing the elements by key, the queue should not be used anywhere else than in the two methods above. 您继续使用映射按键访问元素,不应在上述两种方法之外的任何地方使用该队列。
I had the same problem every once in a while and here is my solution: https://github.com/nlohmann/fifo_map . 我偶尔会遇到同样的问题,这是我的解决方案: https : //github.com/nlohmann/fifo_map 。 It's a header-only C++11 solution and can be used as drop-in replacement for a
std::map
. 它是一个仅限标头的C ++ 11解决方案,可以用作
std::map
替代品。
#include "src/fifo_map.hpp"
// for convenience
using nlohmann::fifo_map;
int main() {
// create fifo_map with template arguments
fifo_map<int, std::string> m;
// add elements
m[2] = "two";
m[3] = "three";
m[1] = "one";
// output the map; will print
// 2: two
// 3: three
// 1: one
for (auto x : m) {
std::cout << x.first << ": " << x.second << "\n";
}
// delete an element
m.erase(2);
// re-add element
m[2] = "zwei";
// output the map; will print
// 3: three
// 1: one
// 2: zwei
for (auto x : m) {
std::cout << x.first << ": " << x.second << "\n";
}
}
Note how the fifo_map
's elements are always printed in the order of the insertion. 请注意
fifo_map
的元素是如何按插入顺序打印的。 Deletion of old elements is not implemented, but this extension should not be too difficult. 删除旧元素没有实现,但这个扩展不应该太难。
#include<iostream>
#include<queue>
using namespace std;
main(){
queue < pair <int,int> > Q; //First use a queue to store the pair wise values
int a,b;
// insert value to the queue as a pair
for (int i=0;i<6;i++){ // i only insert 6 pairs
cin>>a>>b;
if (Q.size()>=3){ // if queue size is 3 than pop up the first value
Q.pop();
}
Q.push(make_pair(a,b)); // insert a new pair into the queue
}
while(!Q.empty()){ // output the pairs on that queue
cout<<Q.front().first<<" "<<Q.front().second<<endl;
Q.pop();
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.