来电订单参考一些信息
现在我有 std::map
. Object
类具有以下函数:int getPriority()
和 void Work()
。现在我浏览地图并由于对象的优先级而想要调用 Work
。
我写了一个非常疯狂的想法:该映射的克隆,但它存储在它的优先级的键中,例如:
myMap["3_key1"] = Object();
myMap["6_key2"] = Object();
myMap["0_key3"] = Object();
它排序并调用在正确的队列中:0_key3; 3_键1; 6_key2
。
但我认为这非常慢。我想用 boost 中的 unordered_map
替换 std::map
因为它快得多。而且没有按键排序。
那么,有什么想法吗?
now I have std::map<std::string, Object> myMap
. Class Object
has funcions: int getPriority()
and void Work()
. Now I go through the map and want to call Work
due to the priority of object.
I wrote very crazy idea: the clone of that map, but it stores in the key it's priority, for example:
myMap["3_key1"] = Object();
myMap["6_key2"] = Object();
myMap["0_key3"] = Object();
It sorts and calling is in right queue: 0_key3; 3_key1; 6_key2
.
But this is very slow, I think. And I want to replace std::map
with unordered_map
from boost because it's a lot faster. And there isn't sorting by key.
So, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Boost.MultiIndex:
我不对它进行任何性能测试,但它肯定更好地表达了您的意图,而且这通常表明性能有所提高。
Use Boost.MultiIndex:
I don't have any performance tests on it, but it certainly better expresses your intent, and that's usually indicative of improved performance anyway.
我认为最简单的方法是两个容器。您当前的键->值映射,以及按对象优先级排序的第二个键堆(可能只使用
priority_queue
作为堆实现)。不过,这会带来困难,优先级可以动态改变。I think the easiest way is two containers. Your current key->value map, and a second heap of keys that orders by priority of the object (possibly just use
priority_queue
as your heap implementation). This would have difficulties of priority can change on the fly though.如果您使用 std::set 或 std::priority_queue 而不是 std::map,您可以定义一个仿函数,或者简单地实现operator<为您的类,以便 std::set 为您按优先级顺序对对象进行排序。
If you use std::set or std::priority_queue instead of std::map, you can define a functor, or simply implement operator< for your class so that the std::set orders the objects in priority order for you.