Difference between std::set and std::priority_queue

前端 未结 4 1728
情书的邮戳
情书的邮戳 2021-01-29 23:14

Since both std::priority_queue and std::set (and std::multiset) are data containers that store elements and allow you to access them in an

4条回答
  •  耶瑟儿~
    2021-01-29 23:51

    std::priority_queue allows to do the following:

    1. Insert an element O(log n)
    2. Get the smallest element O(1)
    3. Erase the smallest element O(log n)

    while std::set has more possibilities:

    1. Insert any element O(log n) and the constant is greater than in std::priority_queue
    2. Find any element O(log n)
    3. Find an element, >= than the one your are looking for O(log n) (lower_bound)
    4. Erase any element O(log n)
    5. Erase any element by its iterator O(1)
    6. Move to previous/next element in sorted order O(1)
    7. Get the smallest element O(1)
    8. Get the largest element O(1)

提交回复
热议问题