How to preallocate(reserve) a priority_queue?

前端 未结 4 963
-上瘾入骨i
-上瘾入骨i 2021-01-01 20:35

How can I preallocate a std::priority_queue with a container of type std::vector?

std::priority_queue

        
4条回答
  •  没有蜡笔的小新
    2021-01-01 21:01

    In general with C++11 you can write a make_reserved function as below.

    #include 
    #include 
    #include 
    #include 
    
    template 
    std::vector make_reserved(const std::size_t n)
    {
      std::vector v;
      v.reserve(n);
      return v;
    }
    
    int main()
    {
      using Q = std::priority_queue>;
      auto q = Q(std::less(), make_reserved(100));
      std::cout << q.size() << std::endl;
    }
    

提交回复
热议问题