Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

c++ - using boost multi_index_container to preserve insertion order

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...