C++_STL-queue(and priority_queue)

C++_STL-queue(and priority_queue)

queue:

template <class T, class Container = deque<T> > class queue;

First in first out queue

Parameters:

T: Element type

Container: the type of the internal underlying container object that stores the element.

1. Constructor std::queue::queue

initialize (1)explicit queue (const container_type& ctnr);
move-initialize (2)explicit queue (container_type&& ctnr = container_type());
allocator (3)template <class Alloc> explicit queue (const Alloc& alloc);
init + allocator (4)template <class Alloc> queue (const container_type& ctnr, const Alloc& alloc);
move-init + allocator (5)template <class Alloc> queue (container_type&& ctnr, const Alloc& alloc);
copy + allocator (6)template <class Alloc> queue (const queue& x, const Alloc& alloc);
move + allocator (7)template <class Alloc> queue (queue&& x, const Alloc& alloc);

1.1 functions

1. Initialize constructor:

Construct a container adapter whose internal container is initialized to a copy of ctnr.

2. Move initialization constructor:

Construct a container adapter whose internal container obtains the value of ctnr by moving it.

3. Allocator constructor:

Construct a container adapter whose internal container is constructed with alloc as a parameter.

4. Initialize with allocator constructor:

Construct a container adapter whose internal container is constructed with cntr and alloc as parameters.

5. Use the allocator constructor for mobile initialization:

Construct a container adapter whose internal container is constructed with std::move(cntr) and alloc as parameters.

6. Copy with allocator constructor:

Construct a container adapter. Its internal container is constructed with the internal container of x as the first parameter and alloc as the second parameter.

7. Move with allocator constructor:

Construct a container adapter. Its internal container is constructed by moving the internal container of x as the first parameter and passing alloc as the second parameter.

// constructing queues
#include <iostream>       // std::cout
#include <deque>          // std::deque
#include <list>           // std::list
#include <queue>          // std::queue

int main ()
{
  std::deque<int> mydeck (3,100);        // deque with 3 elements
  std::list<int> mylist (2,200);         // list with 2 elements

  std::queue<int> first;                 // empty queue
  std::queue<int> second (mydeck);       // queue initialized to copy of deque

  std::queue<int,std::list<int> > third; // empty queue with list as underlying container
  std::queue<int,std::list<int> > fourth (mylist);

  std::cout << "size of first: " << first.size() << '\n';
  std::cout << "size of second: " << second.size() << '\n';
  std::cout << "size of third: " << third.size() << '\n';
  std::cout << "size of fourth: " << fourth.size() << '\n';

  return 0;
}

2,std::queue::back

reference& back();
const_reference& back() const;

2.1 functions

Returns a reference to the last element in the queue. This is the "latest" element in the queue (the last element pushed into the queue).

2.2 return value

A reference to the last element in the queue.

// queue::back
#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myqueue;

  myqueue.push(12);
  myqueue.push(75);   // this is now the back

  myqueue.back() -= myqueue.front();

  std::cout << "myqueue.back() is now " << myqueue.back() << '\n';

  return 0;
}

3,std::queue::emplace

template <class... Args> void emplace (Args&&... args);

3.1 functions

Adds a new element at the end of the queue, after its current last element. This new element is constructed in place with args as an argument to its constructor.

3.2 parameters

constructors parameters

// queue::emplace
#include <iostream>       // std::cin, std::cout
#include <queue>          // std::queue
#include <string>         // std::string, std::getline(string)

int main ()
{
  std::queue<std::string> myqueue;

  myqueue.emplace ("First sentence");
  myqueue.emplace ("Second sentence");

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

  return 0;
}

4,std::queue::empty

bool empty() const;

4.1 functions

Determine whether the queue is empty

4.2 return value

true if the size of the underlying container is 0, otherwise false.

// queue::empty
#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myqueue;
  int sum (0);

  for (int i=1;i<=10;i++) myqueue.push(i);

  while (!myqueue.empty())
  {
     sum += myqueue.front();
     myqueue.pop();
  }

  std::cout << "total: " << sum << '\n';

  return 0;
}

5,std::queue::front

 reference& front();
const_reference& front() const;

5.1 functions

Returns a reference to the queue header element

5.2 return value

Reference to the queue header element

// queue::front
#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myqueue;

  myqueue.push(77);
  myqueue.push(16);

  myqueue.front() -= myqueue.back();    // 77-16=61

  std::cout << "myqueue.front() is now " << myqueue.front() << '\n';

  return 0;
}

6,std::queue::pop

void pop();

6.1 functions

Pop up the queue header element, delete the queue header element, and effectively reduce its size by one.

// queue::push/pop
#include <iostream>       // std::cin, std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myqueue;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    myqueue.push (myint);
  } while (myint);

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

  return 0;
}

7,std::queue::push

void push (const value_type& val);
void push (value_type&& val);

7.1 functions

Inserts a new element at the end of the queue, after its current last element. The content of this new element is initialized to val. This member function effectively calls the member function push of the underlying container object_ back.

7.2 parameters

Insert the initialization value of the element.

// queue::push/pop
#include <iostream>       // std::cin, std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myqueue;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    myqueue.push (myint);
  } while (myint);

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

  return 0;
}

8,std::queue::size

size_type size() const;

8.1 functions

Returns the size of the underlying container

8.2 return value

The size of the underlying container

// queue::size
#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<5; i++) myints.push(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.pop();
  std::cout << "2. size: " << myints.size() << '\n';

  return 0;
}

9,std::queue::swap

void swap (queue& x) noexcept(/*see below*/);

9.1 functions

Exchange the contents of this underlying container and x underlying container

// queue::swap
#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> foo,bar;
  foo.push (10); foo.push(20); foo.push(30);
  bar.push (111); bar.push(222);

  foo.swap(bar);

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

  return 0;
}

priority_queue:

template <class T, class Container = vector<T>,
  class Compare = less<typename Container::value_type> > class priority_queue;

Priority queue

After special design, according to some strict weak sorting standards, its first element is always the largest element among the elements it contains. Those who meet the requirements of comparison are at the end of the team and those who do not meet the requirements are at the head of the team, so the default comparison = less, that is, a < B, B is at the head of the team and a is at the end of the team

Parameters:

T: The type of the element.

Container: the type of the internal underlying container object that stores the element.

Compare: compare function

Be careful not to confuse constructor parameters

1. Constructor

initialize (1)priority_queue (const Compare& comp, const Container& ctnr);
range (2)template <class InputIterator> priority_queue (InputIterator first, InputIterator last, const Compare& comp, const Container& ctnr);
move-initialize (3)explicit priority_queue (const Compare& comp = Compare(),
Container&& ctnr = Container());
move-range (4)template <class InputIterator> priority_queue (InputIterator first, InputIterator last, const Compare& comp, Container&& ctnr = Container());
allocator versions (5)template explicit priority_queue (const Alloc& alloc);
template priority_queue (const Compare& comp, const Alloc& alloc);
template priority_queue (const Compare& comp, const Container& ctnr, const Alloc& alloc);
template priority_queue (const Compare& comp, Container&& ctnr, const Alloc& alloc);
template priority_queue (const priority_queue& x, const Alloc& alloc);
template priority_queue (priority_queue&& x, const Alloc& alloc);

1.1 parameters

comp: comparison object used to sort the heap.

ctnr: container object.

first,last: enter the iterator into the initial and final positions in the sequence. The elements in this sequence are inserted into the underlying container before sorting.

// constructing priority queues
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
#include <vector>         // std::vector
#include <functional>     // 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<rhs);
  }
};

int main ()
{
  int myints[]= {10,60,50,20};

  std::priority_queue<int> first;
  std::priority_queue<int> second (myints,myints+4);
  std::priority_queue<int, std::vector<int>, std::greater<int> >
                            third (myints,myints+4);
  // using mycomparison:
  typedef std::priority_queue<int,std::vector<int>,mycomparison> mypq_type;

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

  return 0;
}

Priority of queue_ There are all queues except front

2,std::priority_queue::top

const_reference top() const;

2.1 functions

Return to priority_ Constant reference to the top element in the queue.

2.2 return value

Constant reference to the top element.

// 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() << '\n';

  return 0;
}

Tags: C++ Autonomous vehicles

Posted on Sat, 18 Sep 2021 07:53:54 -0400 by jgetner