how does the stl's multimap insert respect orderings?

后端 未结 7 2215
灰色年华
灰色年华 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:32

    Unless I've missed something, the standard doesn't provide any such guarantee.

    The most obvious workaround would be to include a sequence number as a secondary key. For example, in your class include a static unsigned long, and each time you create an object to insert in the multimap, put its current value into your object, and increment it. In your object's comparison function, use that counter as the deciding factor for ordering if the data you're currently using as the key compares equal. Note that in this case, each key will be unique, so you can use a map instead of a multimap.

提交回复
热议问题