C++優先キュー
優先キューもキューというデータ構造の1つです.その操作はキューの先進的な先発に限らず、論理(最大値または最小値などでキューを出す)で行うことができます.下地実装:スタック.ここではc++の中の優先キューコンテナ-priority_を紹介しますqueue
テンプレート宣言には3つのパラメータがありますpriority_Queue Typeはデータ型,Containerはデータを保存するコンテナ,Functionalは要素比較方式である.Containerはvector,dequeのような配列で実現するコンテナでなければならないがlistは使用できない.STLのデフォルトはvectorです.比較方法のデフォルトはoperator<ですので、後ろの2つのパラメータをデフォルトにすると、優先キューは大きなトップスタックで、キューヘッダ要素が最大になります.
基本操作:
ここで優先キューに格納10個の乱数は、デフォルトでは優先キューが大トップスタックで実装されているため、毎回大から小の順にキューからデキューされることがわかります.ヒープをカスタマイズするには、次の手順に従います.
≪カスタム比較関係|Customize Compare Relationships|emdw≫:カスタム・タイプの場合、operator<または自己書込み関数を再ロードする必要があります.
アナログスタック実装
詳細とpriority_queueの関連知識の後続は学習が深くなるにつれて引き続き補充して完備します!
テンプレート宣言には3つのパラメータがありますpriority_Queue Typeはデータ型,Containerはデータを保存するコンテナ,Functionalは要素比較方式である.Containerはvector,dequeのような配列で実現するコンテナでなければならないがlistは使用できない.STLのデフォルトはvectorです.比較方法のデフォルトはoperator<ですので、後ろの2つのパラメータをデフォルトにすると、優先キューは大きなトップスタックで、キューヘッダ要素が最大になります.
基本操作:
#include
using namespace std;
int main()
{
srand(time(NULL));
priority_queue<int> pq;
for(int i=0; i<10; i++)
{
int num = rand()%100;
pq.push(num);
}
while(!pq.empty())
{
//queue front(),
cout<<pq.top()<<" ";
pq.pop();
}
return 0;
}
ここで優先キューに格納10個の乱数は、デフォルトでは優先キューが大トップスタックで実装されているため、毎回大から小の順にキューからデキューされることがわかります.ヒープをカスタマイズするには、次の手順に従います.
#include
using namespace std;
int main()
{
srand(time(NULL));
priority_queue<int,vector<int>,greater<int> > pq;
for(int i=0; i<10; i++)
{
int num = rand()%100;
pq.push(num);
}
while(!pq.empty())
{
cout<<pq.top()<<" ";
pq.pop();
}
return 0;
}
≪カスタム比較関係|Customize Compare Relationships|emdw≫:カスタム・タイプの場合、operator<または自己書込み関数を再ロードする必要があります.
#include
using namespace std;
bool Mycmp(int a,int b)
{
return a%10<b%10;
}
int main()
{
srand(time(NULL));
priority_queue<int,vector<int>,function<bool(int,int)>> pq(Mycmp);
for(int i=0; i<10; i++)
{
int num = rand()%100;
pq.push(num);
}
while(!pq.empty())
{
cout<<pq.top()<<" ";
pq.pop();
}
return 0;
}
#include
using namespace std;
struct Node
{
int x, y;
Node( int a= 0, int b= 0 ):
x(a), y(b) {}
};
bool operator<( Node a, Node b )
{
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
int main()
{
srand(time(NULL));
priority_queue<Node> pq;
for(int i=0; i<10; i++)
{
int num1 = rand()%10;
int num2 = rand()%10;
pq.push(Node(num1,num2));
}
while(!pq.empty())
{
cout<<pq.top().x<<" "<<pq.top().y<<endl;
pq.pop();
}
return 0;
}
#include
using namespace std;
struct Node
{
int x, y;
Node( int a= 0, int b= 0 ):
x(a), y(b) {}
};
struct cmp
{
bool operator() ( Node a, Node b )
{
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
};
int main()
{
srand(time(NULL));
priority_queue<Node, vector<Node>, cmp> pq;
for(int i=0; i<10; i++)
{
int num1 = rand()%10;
int num2 = rand()%10;
pq.push(Node(num1,num2));
}
while(!pq.empty())
{
cout<<pq.top().x<<" "<<pq.top().y<<endl;
pq.pop();
}
return 0;
}
アナログスタック実装
#include
#include
#include
using namespace std;
class priority_queue
{
private:
vector<int> data;
public:
void push( int t )
{
data.push_back(t);
push_heap( data.begin(), data.end());
}
void pop()
{
pop_heap( data.begin(), data.end() );
data.pop_back();
}
int top()
{
return data.front();
}
int size()
{
return data.size();
}
bool empty()
{
return data.empty();
}
};
int main()
{
priority_queue test;
test.push( 3 );
test.push( 5 );
test.push( 4 );
test.push( 2 );
test.push( 1 );
while( !test.empty() )
{
cout << test.top() << endl;
test.pop();
}
return 0;
}
詳細とpriority_queueの関連知識の後続は学習が深くなるにつれて引き続き補充して完備します!