• (堆)priority_queue


    注意结合queue的用法记忆

    top()(注意这个在queue中没有,和queue中的front()一样)

    // priority_queue::top
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> mypq;
    
      mypq.push(10);
      mypq.push(20);
      mypq.push(15);
    
      std::cout << "mypq.top() is now " << mypq.top() << '
    ';
    
      return 0;
    }

    Output:

    mypq.top() is now 20
    

    swap()

    // priority_queue::swap
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> foo,bar;
      foo.push (15); foo.push(30); foo.push(10);
      bar.push (101); bar.push(202);
    
      foo.swap(bar);
    
      std::cout << "size of foo: " << foo.size() << '
    ';
      std::cout << "size of bar: " << bar.size() << '
    ';
    
      return 0;
    }
    

    Output:

    
    size of foo: 2
    size of bar: 3
    


     

    size()

    // priority_queue::size
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> myints;
      std::cout << "0. size: " << myints.size() << '
    ';
    
      for (int i=0; i<5; i++) myints.push(i);
      std::cout << "1. size: " << myints.size() << '
    ';
    
      myints.pop();
      std::cout << "2. size: " << myints.size() << '
    ';
    
      return 0;
    }
    
    


     

    Output
    0. size: 0

    1. size: 5

    2. size: 4

    push()

    // priority_queue::push/pop
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> mypq;
    
      mypq.push(30);
      mypq.push(100);
      mypq.push(25);
      mypq.push(40);
    
      std::cout << "Popping out elements...";
      while (!mypq.empty())
      {
         std::cout << ' ' << mypq.top();
         mypq.pop();
      }
      std::cout << '
    ';
    
      return 0;
    }

    Output     Popping out elements... 100 40 30 25
    


    pop()

    // priority_queue::push/pop
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> mypq;
    
      mypq.push(30);
      mypq.push(100);
      mypq.push(25);
      mypq.push(40);
    
      std::cout << "Popping out elements...";
      while (!mypq.empty())
      {
         std::cout << ' ' << mypq.top();
         mypq.pop();
      }
      std::cout << '
    ';
    
      return 0;
    }
    
    


    Output   Popping out elements... 100 40 30 25
    


    empty()

    / priority_queue::empty
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    
    int main ()
    {
      std::priority_queue<int> mypq;
      int sum (0);
    
      for (int i=1;i<=10;i++) mypq.push(i);
    
      while (!mypq.empty())
      {
         sum += mypq.top();
         mypq.pop();
      }
    
      std::cout << "total: " << sum << '
    ';
    
      return 0;
    }
    

    Output    total: 55
    


    emplace()(不常用)

    // priority_queue::emplace
    #include <iostream>       // std::cout
    #include <queue>          // std::priority_queue
    #include <string>         // std::string
    
    int main ()
    {
      std::priority_queue<std::string> 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 << '
    ';
    
      return 0;
    }  Edit & Run 
    
    


    Output        mypq contains: strawberry pear orange apple

  • 相关阅读:
    Fleury算法
    2012 MultiUniversity Training Contest 2
    高斯若尔当消元法(线性代数)
    hdu4307(最小割)
    hdu4305(生成树计数)
    hdu4318(最短路)
    hdu4309(网络流)
    hdu4313(生成树)
    生成树计数
    MYSQL 远程登录及常用命令
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432099.html
Copyright © 2020-2023  润新知