C++/Queue Stack/priority queue
Содержание
- 1 A priority_queue: size(), top(), empty(), pop()
- 2 Define your function to Prioritize a priority_queue
- 3 priority_queue of double
- 4 Priority Queue Opertions: empty(), pop(), top(), push()
- 5 priority_queue: pop
- 6 priority_queue: push and size
- 7 priority_queue: push, pop, top, empty
- 8 priority_queue: top
- 9 Using a Priority Queue
A priority_queue: size(), top(), empty(), pop()
#include <queue>
#include <iostream>
int main ()
{
using namespace std;
priority_queue <int> pqIntegers;
pqIntegers.push (10);
pqIntegers.push (5);
pqIntegers.push (-1);
pqIntegers.push (20);
cout << "The queue contains " << pqIntegers.size () << " elements";
cout << endl;
cout << "Element at the top: " << pqIntegers.top () << endl << endl;
while (!pqIntegers.empty ())
{
cout << "Deleting the topmost element: " << pqIntegers.top ();
cout << endl;
pqIntegers.pop ();
}
return 0;
}
Define your function to Prioritize a priority_queue
#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Prioritize {
public:
int operator() ( const pair<string, unsigned int>& p1,const pair<string, unsigned int>& p2 ) {
return p1.second < p2.second;
}
};
int main()
{
priority_queue< pair< string, unsigned int >,vector <pair< string, unsigned int > >, Prioritize > pq;
pq.push( pair<string, int>( "A", 2) );
pq.push( pair<string, int>( "B", 10 ) );
pq.push( pair<string, int>( "C", 1 ) );
while ( !pq. empty() ) {
cout << pq.top().first << endl;
pq.pop();
}
return 0;
}
priority_queue of double
#include <iostream>
using std::cout;
using std::endl;
#include <queue>
int main()
{
std::priority_queue< double > priorities;
priorities.push( 3.2 );
priorities.push( 9.8 );
priorities.push( 5.4 );
cout << "Popping from priorities: ";
while ( !priorities.empty() )
{
cout << priorities.top() << " ";
priorities.pop();
}
cout << endl;
return 0;
}
/*
Popping from priorities: 9.8 5.4 3.2
*/
Priority Queue Opertions: empty(), pop(), top(), push()
#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Prioritize {
public:
int operator() ( const pair<string, unsigned int>& p1,const pair<string, unsigned int>& p2 ) {
return p1.second < p2.second;
}
};
int main()
{
priority_queue< pair< string, unsigned int >,vector <pair< string, unsigned int > >, Prioritize > pq;
pq.push( pair<string, int>( "A", 2) );
pq.push( pair<string, int>( "B", 10 ) );
pq.push( pair<string, int>( "C", 1 ) );
while ( !pq. empty() ) {
cout << pq.top().first << endl;
pq.pop();
}
return 0;
}
priority_queue: pop
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int thedata[] = {45, 34, 56, 27, 71, 50, 62};
priority_queue<int> pq;
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Pushing 4 elements " << endl;
for (int i = 0; i < 4; ++i)
pq.push(thedata[i]);
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Popping 3 elements " << endl;
for (int i = 0; i < 3; ++i) {
cout << pq.top() << endl;
pq.pop();
}
cout << "The priority_queue size is now " << pq.size() << endl;
return 0;
}
/*
The priority_queue size is now 0
Pushing 4 elements
The priority_queue size is now 4
Popping 3 elements
56
45
34
The priority_queue size is now 1
*/
priority_queue: push and size
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int thedata[] = {45, 34, 56, 27, 71, 50, 62};
priority_queue<int> pq;
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Pushing 4 elements " << endl;
for (int i = 0; i < 4; ++i)
pq.push(thedata[i]);
cout << "The priority_queue size is now " << pq.size() << endl;
return 0;
}
/*
The priority_queue size is now 0
Pushing 4 elements
The priority_queue size is now 4
*/
priority_queue: push, pop, top, empty
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<float> q;
// insert three elements into the priority queue
q.push(66.6);
q.push(22.2);
q.push(44.4);
// read and print two elements
cout << q.top() << " ";
q.pop();
cout << q.top() << endl;
q.pop();
// insert three more elements
q.push(11.1);
q.push(55.5);
q.push(33.3);
// skip one element
q.pop();
// pop and print remaining elements
while (!q.empty()) {
cout << q.top() << " ";
q.pop();
}
cout << endl;
}
/*
66.6 44.4
33.3 22.2 11.1
*/
priority_queue: top
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int thedata[] = {45, 34, 56, 27, 71, 50, 62};
priority_queue<int> pq;
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Pushing 4 elements " << endl;
for (int i = 0; i < 4; ++i)
pq.push(thedata[i]);
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Popping 3 elements " << endl;
for (int i = 0; i < 3; ++i) {
cout << pq.top() << endl;
pq.pop();
}
cout << "The priority_queue size is now " << pq.size() << endl;
cout << "Popping all elements" << endl;
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}
cout << "The priority_queue size is now " << pq.size() << endl;
return 0;
}
/*
The priority_queue size is now 0
Pushing 4 elements
The priority_queue size is now 4
Popping 3 elements
56
45
34
The priority_queue size is now 1
Popping all elements
27
The priority_queue size is now 0
*/
Using a Priority Queue
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Message
{
public:
Message( string message = "A",string source = "B", int security_level = 0 );
bool operator<( const Message& rhs ) const;
string message() const;
int security_level() const;
string source() const;
private:
string message_, source_;
int security_level_;
};
inline
Message::Message( string message, string source, int security_level )
: message_( message ), source_( source ),
security_level_( security_level )
{}
bool Message::operator<( const Message& rhs ) const
{ return security_level() < rhs.security_level(); }
string Message::message() const
{ return message_; }
string Message::source() const
{ return source_; }
int Message::security_level() const
{ return security_level_; }
int main( )
{
const char* message[] = { "A","B", "C","D","E","F","G" };
const char* source[] = { "a", "b", "c","d", "e", "f", "G" };
const int security_level[] = { 0, 1, 3, 2, 6, 7, 8 };
const int num_messages = sizeof( security_level ) / sizeof( security_level[0] );
priority_queue<Message> messages;
for( int i = 0; i < num_messages; ++i ){
cout << "Event " << (i+1) << ": Security level - "
<< security_level[i] << "\n\t" << source[i] << " reports "
<< message[i] << endl;
messages.push( Message( message[i], source[i],security_level[i] ) );
}
while( !messages.empty() )
{
cout << "Security level - "
<< messages.top().security_level() << "\n\t"
<< messages.top().source() << " reports "
<< messages.top().message() << endl;
messages.pop();
}
}