cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : priority_queue : push
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
priority_queue
priority_queue::priorit...
member functions:
· priority_queue::empty
· priority_queue::pop
· priority_queue::push
· priority_queue::size
· priority_queue::top

-

priority_queue::push public member function
void push ( const T& x );

Insert element

Inserts a new element in the priority_queue. The content of this new element is initialized to a copy of x.

This member function effectively calls the member function push_back of the underlying container object, and then calls the push_heap algorithm to keep the heap property of priority_queues.

Parameters

x
Value to be copied to the new element.
T is the first template parameter (the type of the elements stored in the stack).

Return value

none

Example

// priority_queue::push/pop
#include <iostream>
#include <queue>
using namespace std;

int main ()
{
  priority_queue<int> mypq;

  mypq.push(30);
  mypq.push(100);
  mypq.push(25);
  mypq.push(40);

  cout << "Popping out elements...";
  while (!mypq.empty())
  {
     cout << " " << mypq.top();
     mypq.pop();
  }
  cout << endl;

  return 0;
}

Output:

Popping out elements... 100 40 30 25


Complexity

Constant (in the priority_queue). Although notice that push_heap operates in logarithmic time.

See also

priority_queue::pop Remove top element (public member function)
priority_queue::size Return size (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us