Does the C++ standard library have a set ordered by insertion order?

前端 未结 6 390
春和景丽
春和景丽 2021-01-11 09:30

Does the C++ standard library have an \"ordered set\" datastructure? By ordered set, I mean something that is exactly the same as the ordinary std::set but that

6条回答
  •  既然无缘
    2021-01-11 10:04

    No, it does not.

    Such a container presumably would need two different iterators, one to iterate in the order defined by the order of adding, and another to iterate in the usual set order. There's nothing of that kind in the standard libraries.

    One option to simulate it is to have a set of some type that contains an intrusive linked list node in addition to the actual data you care about. After adding an element to the set, append it to the linked list. Before removing an element from the set, remove it from the linked list. This is guaranteed to be OK, since pointers to set elements aren't invalidated by any operation other than removing that element.

提交回复
热议问题