using boost multi_index_container to preserve insertion order

前端 未结 2 578
情书的邮戳
情书的邮戳 2020-12-19 11:17

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

相关标签:
2条回答
  • 2020-12-19 12:01

    How about a

    map<int, vector<string> >
    

    or

    map<int, list<string> >
    

    @Kirill: Good answer. I suspect Boost's random_access can be quite slow, as it will force all strings for all keys to be maintained in a single contiguous structure. Whereas the questioner simply wants order to be preserved within each key's set of mapped values.

    0 讨论(0)
  • 2020-12-19 12:04

    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;
    
    0 讨论(0)
提交回复
热议问题