Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

c++11 - Converting a priority queue to vector of ints in C++

I am trying to convert a min-heap priority queue to return as a vector of ints. Is this type conversion is possible in C++;

question from:https://stackoverflow.com/questions/65911007/converting-a-priority-queue-to-vector-of-ints-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The C++ std::priority_queue uses a container for storage https://en.cppreference.com/w/cpp/container/priority_queue

By default it is class Container = std::vector<T>

Is this what you want to get access to ? If so, though luck:

C, the underlying container (protected member object)

It is not exposed. You could derive from priority_queue and expose it, if needed. But you post is thin as to what you are actually trying to do.

Keep in mind that:

  • The container doesn't store sorted elements. It is a heap, so only the first element will be correct. It might have little actual use.

This answer discusses accessing the Container: Is there a way to access the underlying container of STL container adaptors?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...