priority_queue member function
阅读原文时间:2023年07月10日阅读:1

没有优先队列的dijkstra不算真的dijkstra

所以我又回来补常识了

<1>priority_queue::emplace

<7>priority_queue::top

// priority_queue::emplace
#include // std::cout
#include // std::priority_queue
#include // std::string

int main ()
{
std::priority_queue mypq;

mypq.emplace("orange");
mypq.emplace("strawberry");
mypq.emplace("apple");
mypq.emplace("pear");

std::cout << "mypq contains:";
while (!mypq.empty())
{
std::cout << ' ' << mypq.top();
mypq.pop();
}
std::cout << '\n';

return ;
}
//Ooutput:mypq contains: strawberry pear orange apple

<2>priority_queue::empty

<3>priority_queue::pop

<4>priority_queue::push

// priority_queue::push/pop
#include // std::cout
#include // std::priority_queue

int main ()
{
std::priority_queue mypq;

mypq.push();
mypq.push();
mypq.push();
mypq.push();

std::cout << "Popping out elements…";
while (!mypq.empty())
{
std::cout << ' ' << mypq.top();
mypq.pop();
}
std::cout << '\n';

return ;
}
//Output:Popping out elements… 100 40 30 25

<5>priority_queue::size

<6>priority_queue::swap

// priority_queue::swap
#include // std::cout
#include // std::priority_queue

int main ()
{
std::priority_queue foo,bar;
foo.push (); foo.push(); foo.push();
bar.push (); bar.push();

foo.swap(bar);

std::cout << "size of foo: " << foo.size() << '\n';
std::cout << "size of bar: " << bar.size() << '\n';

return ;

/*Output:
size of foo: 2
size of bar: 3*/

然后还有个自定义排序来着,到时候再摸。