2018-12-21之优先队列priority_queue

2018年12月20日21:45:02

STL之vector

vector pile[maxn]

是确定行数,不定列数的二维数组,比链表数组更容易实现,而且使用的空间更少

2018年12月21日19:44:05

优先队列和sort中的comp

Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

// sort algorithm example
#include      // std::cout
#include     // std::sort
#include        // std::vector

bool myfunction (int i,int j) { return (i myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Output

myvector contains: 12 26 32 33 45 53 71 80

priority_queue

// constructing priority queues
#include        // std::cout
#include           // std::priority_queue
#include          // std::vector
#include      // std::greater

class mycomparison
{
  bool reverse;
public:
  mycomparison(const bool& revparam=false)
    {reverse=revparam;}
  bool operator() (const int& lhs, const int&rhs) const
  {
    if (reverse) return (lhs>rhs);
    else return (lhs first;
  std::priority_queue second (myints,myints+4);
  std::priority_queue, std::greater >
                            third (myints,myints+4);
  // using mycomparison:
  typedef std::priority_queue,mycomparison> mypq_type;

  mypq_type fourth;                       // less-than comparison
  mypq_type fifth (mycomparison(true));   // greater-than comparison

  return 0;
}
priority_queue的《》中的第二个还是不太理解20181221203443.png

你可能感兴趣的:(2018-12-21之优先队列priority_queue)