本文整理汇总了C++中PriorityQueue::Push方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::Push方法的具体用法?C++ PriorityQueue::Push怎么用?C++ PriorityQueue::Push使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::Push方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
// Builds the tree
TreeNode<SymbolPriority>* MakeTree(const string& message)
{
char currentChar;
vector<SymbolPriority> temp;
PriorityQueue<TreeNode<SymbolPriority>*> myPriorityQueue;
for (int i = 0; i < int(message.size()); i++)
{
currentChar = message[i];
if ( temp.empty() )
temp.push_back( SymbolPriority(currentChar, 1) );
else if ( characterExists(temp, currentChar) )
{
for (int c = 0; c < int (temp.size()); i++)
if (currentChar == temp[i].Symbol)
temp[i].Priority++;
}
else
temp.push_back( SymbolPriority(currentChar, 1) );
}
for (int i = 0; i < int(temp.size()); i++)
{
if (myPriorityQueue.GetSize() <= 1)
myPriorityQueue.Push( new TreeNode<SymbolPriority>( temp[i]) );
else
{
char aChar;
TreeNode<SymbolPriority>* tempNode;
// create a new TreeNode<SymbolPriority>* and
// make the first popped element its left child
// make the second popped element its right child
// set its value to the sum of its left and right child priorities
tempNode->Left = myPriorityQueue.Top();
aChar = myPriorityQueue.Top()->Data.Priority;
myPriorityQueue.Pop();
tempNode->Right = myPriorityQueue.Top();
aChar += myPriorityQueue.Top()->Data.Priority;
myPriorityQueue.Pop();
myPriorityQueue.Push( tempNode );
}
}
return myPriorityQueue.Top();
}
示例2: Prim
bool Prim(SGraph& graph, SGraph& result){
SGraph t_g = graph;
int num_v = graph.GetNumVertices();
result.Clear();
if(!graph.IsConnected()){
return false;
}
Vector<SGraph::Edge> unroll_edges;
graph.UnrollEdges(unroll_edges);
Algorithms::Sorting::HeapSort(unroll_edges, unroll_edges);
PriorityQueue<SGraph::Edge> adjacencies;
Vector<int> v_adjac;
SGraph::Edge e = unroll_edges[0];
SGraph::Edge t(0,0);
t_g.DeleteEdge(e);
result.AddEdge(e);
while(result.GetNumEdges() < (num_v -1)){
t_g.FindAdjacencyVertices(e.head, v_adjac);
for(int i = 0; i < v_adjac.Size(); i++){
if(result.HasVertex(v_adjac[i])) continue;
t.head = e.head;
t.tail = v_adjac[i];
t.weight = t_g.GetEdgeW(t.head, t.tail);
adjacencies.Push(t);
}
t_g.FindAdjacencyVertices(e.tail, v_adjac);
for(int i = 0; i < v_adjac.Size(); i++){
if(result.HasVertex(v_adjac[i])) continue;
t.head = v_adjac[i];
t.tail = e.tail;
t.weight = t_g.GetEdgeW(t.head, t.tail);
adjacencies.Push(t);
}
e = adjacencies.Head();
result.AddEdge(e);
t_g.DeleteEdge(e);
adjacencies.Pop();
}
return true;
}
示例3: Test
void Test()
{
int a[] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 };
PriorityQueue<int,Greater> pq;
pq.Push(10);
pq.Push(11);
pq.Push(13);
pq.Push(12);
pq.Push(16);
pq.Push(18);
pq.Push(15);
pq.Push(17);
pq.Push(14);
pq.Push(19);
pq.Print();
}
示例4: main
int main(int argc, char **argv)
{
// using PriorityQueue = pg::heap::Binary<int>;
using PriorityQueue = pg::heap::Pairing<int>;
// using PriorityQueue = StdWrapper<int>;
test("Basic Push/Pop", []()
{
PriorityQueue pq;
pq.Push(10);
pq.Push(5);
pq.Push(8);
pq.Push(12);
pq.Push(1);
pq.Push(90);
pq.Push(9);
assertEquals(1, pq.Pop());
assertEquals(5, pq.Pop());
assertEquals(8, pq.Pop());
assertEquals(9, pq.Pop());
assertEquals(10, pq.Pop());
assertEquals(12, pq.Pop());
assertEquals(90, pq.Pop());
assertEquals(true, pq.Empty());
});
test("Update value", []()
{
PriorityQueue pq;
pq.Push(10);
pq.Push(5);
pq.Push(8);
pq.Push(12);
pq.Push(1);
pq.Push(90);
pq.Push(9);
pq.Update(70, 5);
pq.Update(7, 12);
assertEquals(1, pq.Pop());
assertEquals(7, pq.Pop());
assertEquals(8, pq.Pop());
assertEquals(9, pq.Pop());
assertEquals(10, pq.Pop());
assertEquals(70, pq.Pop());
assertEquals(90, pq.Pop());
assertEquals(true, pq.Empty());
});
test("Performance", []()
{
constexpr auto size = 1000000;
std::default_random_engine random_engine(time(nullptr));
std::uniform_int_distribution<int> distribution(1,9999999);
auto rand = std::bind ( distribution, random_engine );
std::vector<int> test;
PriorityQueue pq;
for(int i = 0 ; i < size; i++)
pq.Push(rand());
while(!pq.Empty())
test.push_back(pq.Pop());
assertEquals(true, std::is_sorted(test.begin(), test.end()));
});
return 0;
}