禁用map/hash_map插入排序

chemo 2007-03-29 02:22:58
我想在向map/hash_map中添加元素时不要让它排序,以后在用迭代器遍历时能保证向插入的先被遍历到,后插入的后遍历到,该如何做?
...全文
653 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bladeIII 2007-03-29
  • 打赏
  • 举报
回复
有意思的是,刚才忽略了一点,对于VC的hash_map实现,如果
采取fuctor做法,hash_map在单个桶内可以做到按照插入次序排序,
但是对于整体,在hash_map大小不能确定的情况下,还是无法控制
整个元素序列的顺序的。这一点在MSDN上有说明:

The actual order of elements in the controlled sequence depends on the hash function, the ordering function, and the current size of the hash table stored in the container object. You cannot determine the current size of the hash table, so you cannot in general predict the order of elements in the controlled sequence. Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that had specifically pointed at the removed elements.

而对于sgi 的hash_map stl,并没有提供大小比较的顺序函数。
healer_kx 2007-03-29
  • 打赏
  • 举报
回复
写个Functor,让它们比较的结果都一样就行了。
taodm 2007-03-29
  • 打赏
  • 举报
回复
不排序还叫map吗。你直接用list、queue就可以了。
bladeIII 2007-03-29
  • 打赏
  • 举报
回复
可以自己写一个functor,给每个插入对象增加个插入ID,表示插入的次序,
按照插入对象次序ID来排序,这样就可以达到先插入在前,后插入在后。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧