need STL set in insertion order

后端 未结 6 435
谎友^
谎友^ 2020-12-31 02:27

How to store elements in set in insertion order. for example.

setmyset;

myset.insert(\"stack\");
myset.insert(\"overflow\");

6条回答
  •  猫巷女王i
    2020-12-31 03:01

    One way is to use two containers, a std::deque to store the elements in insertion order, and another std::set to make sure there are no duplicates.

    When inserting an element, check if it's in the set first, if yes, throw it out; if it's not there, insert it both in the deque and the set.

    One common scenario is to insert all elements first, then process(no more inserting), if this is the case, the set can be freed after the insertion process.

提交回复
热议问题