how does the stl's multimap insert respect orderings?

后端 未结 7 2176
灰色年华
灰色年华 2020-12-03 21:15

I have some data which come with a integer index. I am continuous generating new data which needs to added to the collection of data I have, sorted by that index, at the sa

7条回答
  •  执笔经年
    2020-12-03 21:21

    It sounds like multimap is what you need...

    However, I also need data with the same index to be kept in the order in which it was inserted, in this case meaning that when I iterate through the data I get to the earlier data before the later data.

    It will be in the order of the index. If the index increases over time as you insert more items, then yes because of the nature of your index, the "earlier" data will come first.

    Otherwise, no. You could keep two multimaps to the same data. One kept in order of index, the other in order of insertion time:

    std::multimap orderedByIndex;
    std::multimap orderedByInsertionTime;
    

    Giving you the ability to iterate the map both ways.

    (Edit I'm reading this to mean you want to sometimes iterate by index or sometimes iterate by insertion time, but see the above answer if you mean first index then insertion time.)

提交回复
热议问题