使用boost的multi_index_container至preserve插入顺序 [英] using boost multi_index_container to preserve insertion order

查看:98
本文介绍了使用boost的multi_index_container至preserve插入顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初开始使用的一个的std :: multimap中来存储使用相同的密钥多个值,但是后来我发现,它没有preserve插入使用相同的密钥值中订购。 <一href=\"http://stackoverflow.com/questions/1595270/how-does-the-stls-multimap-insert-respect-orderings/1595473#1595473\">This答案声称,它可以用的boost :: multi_index :: multi_index_container的来完成,但没有给出例子。通过文档来看,有没有这种使用中的例子,我不能使你应该如何使用这个东西正面或反面。我已经开始期待从较少使用Boost库文档质量很差,但是这需要的蛋糕。任何人都可以点我的教程或示例,说明它曾经我想要的方式,甚至可能提供一个例子自己呢?

I initially started out using a std::multimap to store many values with the same key, but then I discovered that it doesn't preserve the insertion order among values with the same key. This answer claims it can be done with boost::multi_index::multi_index_container, but gives no example. Looking through the docs, there are no examples of that usage, and I can't make heads or tails of how you're supposed to use this thing. I have come to expect poor documentation from the lesser-used boost libraries, but this takes the cake. Can anyone point me to a tutorial or example that shows it used the way I want, or perhaps even provide an example themselves?

推荐答案

您可以通过使用实现这个的boost :: multi_index 有两个指标: ordered_non_unique (允许使用相同的密钥值)和<一个href=\"http://www.boost.org/doc/libs/1_43_0/libs/multi_index/doc/tutorial/indices.html#rnd_indices\"><$c$c>random_access(which将保持插入顺序)。

You could achieve this by using boost::multi_index with two indices: ordered_non_unique(which allows values with the same key) and random_access(which will keep the insertion order).

struct some {
  long key;
  int data;
  int more_data;
  // etc.  
};

typedef multi_index_container<
  some, 
  indexed_by<    
    random_access<>,  // keep insertion order
    ordered_non_unique< member<some, long, &some::key> >
  > 
> some_mic_t;

这篇关于使用boost的multi_index_container至preserve插入顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆