What is the underlying data structure of a STL set in C++?

后端 未结 7 918
自闭症患者
自闭症患者 2020-11-29 01:11

I would like to know how a set is implemented in C++. If I were to implement my own set container without using the STL provided container, what would be the best way to go

7条回答
  •  自闭症患者
    2020-11-29 01:27

    You could implement a binary search tree by first defining a Node struct:

    struct Node
    {
      void *nodeData;
      Node *leftChild;
      Node *rightChild;
    }
    

    Then, you could define a root of the tree with another Node *rootNode;

    The Wikipedia entry on Binary Search Tree has a pretty good example of how to implement an insert method, so I would also recommend checking that out.

    In terms of duplicates, they are generally not allowed in sets, so you could either just discard that input, throw an exception, etc, depending on your specification.

提交回复
热议问题