本文整理汇总了C++中PriorityQueue::dequeue方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::dequeue方法的具体用法?C++ PriorityQueue::dequeue怎么用?C++ PriorityQueue::dequeue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::dequeue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: buildTree
Node* buildTree(Vector <int> & weightOfChars, char & delimiter){
PriorityQueue<Node*> weightsOfNodes; // declare a PriorityQueue for simple sorting weights of nodes
bool flag = 1; // set flag to prevent overwriting a delimiter
for(int i = 0; i < weightOfChars.size(); ++i){
if(weightOfChars[i] > 0){
Node* node = new Node((i - 128), weightOfChars[i]);
weightsOfNodes.enqueue(node, weightOfChars[i]);
}
else if((flag && (i < 128)) || (flag && (i > 159))){ // looking for a character that is not a control code and has a number of occurences which is equal zero
delimiter = i;
flag = 0;
}
}
// if file is empty the pointer of root of the tree is a NULL
if(weightsOfNodes.isEmpty()){
return NULL;
}
while (weightsOfNodes.size() > 1) {
Node* lNode = weightsOfNodes.dequeue();
Node* rNode = weightsOfNodes.dequeue();
Node* newNode = new Node(lNode, rNode);
weightsOfNodes.enqueue(newNode, newNode->weight);
}
Node* root = weightsOfNodes.dequeue();
return root;
}
示例2: main
int main(){
PriorityQueue<int> pq;
std::cout << pq.size() << std::endl;
pq.enqueue(1);
pq.present();
pq.enqueue(5);
pq.present();
pq.enqueue(9);
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
pq.enqueue(7);
pq.present();
pq.enqueue(3);
pq.present();
pq.enqueue(0);
pq.enqueue(10);
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
pq.dequeue();
pq.present();
pq.dequeue();
pq.present();
std::cout << pq.size() << " - " << pq.peek() << std::endl;
return 0;
}
示例3: main
int main() {
PriorityQueue<int> *testQueue = new PriorityQueue<int>(3);
// push a few random int's
testQueue->enqueue(4);
testQueue->enqueue(7);
testQueue->enqueue(1);
// queue shouldn't be empty; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
// pop items (more items than were pushed)
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
cout << testQueue->dequeue() << endl;
// should be empty now; check this
if(testQueue->isEmpty()) {
cout << "The queue is empty!" << endl;
}
return 0;
}
示例4: main
int main()
{
PriorityQueue<int> *temp = new PriorityQueue<int>();
temp->enqueue(1, 3);
temp->enqueue(2, 9);
temp->enqueue(3, 2);
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
cout << temp->dequeue() << endl;
delete temp;
return 0;
}
示例5: pathBuilderToStart
vector<Node *> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
for (Vertex* node : graph.getNodeSet()){ //initiate all nodes with an inf. cost
node->cost = INFINITY;
}
PriorityQueue<Vertex*> pQueue;
start->cost = 0;
pQueue.enqueue(start, start->cost); //start of search
while (!pQueue.isEmpty()){
Vertex* v = pQueue.dequeue();
v->visited = true;
v->setColor(GREEN);
if (v == end){
return pathBuilderToStart(start, v); //same as the one bfs uses
}else{
for(Arc* edge : graph.getEdgeSet(v)){ //Go through each edge from the Vertex v, counting the cost for each newly visited vertex
Vertex* next = edge->finish;
if (!next->visited){
double cost = v->cost + edge->cost;
if (cost < next->cost){ //found a lesser cost, what dijkstra is all about
next->setColor(YELLOW);
next->cost = cost;
next->previous = v;
pQueue.enqueue(next, cost);
//pQueue.changePriority(next, cost); Only do this if next already is in the queue, but this should not ever occur?
}
}
}
}
}
return path;
}
示例6: aStar
/*
* Method: aStar
* Parameters: BasicGraph graph by reference
* Vertex pointer variable for path start
* Vertex pointer variable for path end
* - - - - - - - - - - - - - - - - - - - - - - - - - -
* Returns a Vector of Vertex pointer variables for which
* each index corresponds to a step in the path between
* the path's start and end points, if any exists.
* This function uses A*'s algorithm, which evaluates
* the estimated total cost of all neighbors' paths to the end
* from the starting vertex before continuing. It differs from Dijkstra's
* in its prioritization of location based on their estimated total cost
* or distance from the starting point, as opposed to the current cost
* up to that point. Because A* also orients subsequent vertices
* to their earlier parent, this function uses a helper function to
* rewind the shortest route from end to start, if applicable,
* returning an empty vector if not.
*/
Vector<Vertex*> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
Vector<Vertex*> path;
PriorityQueue<Vertex*> pqueue;
pqueue.enqueue(start, heuristicFunction(start, end));
while(!pqueue.isEmpty()) {
Vertex* current = pqueue.dequeue();
visitVertex(current);
if(BasicGraph::compare(current, end) == 0) break;
for(Edge* edge : current->edges) {
double cost = current->cost + edge->cost;
if(!edge->finish->visited &&
edge->finish->getColor() != YELLOW) {
enqueueVertex(edge->finish, current, cost);
pqueue.enqueue(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
if(edge->finish->getColor() == YELLOW &&
cost < edge->finish->cost) {
enqueueVertex(edge->finish, current, cost);
pqueue.changePriority(edge->finish, cost +
heuristicFunction(edge->finish, end));
}
}
}
determinePath(start, end, path);
return path;
}
示例7: main
int main()
{
PriorityQueue<TaskData> taskPQ; // Priority queue of tasks
TaskData task; // Task
int simLength, // Length of simulation (minutes)
minute, // Current minute
numPtyLevels = 2, // Number of priority levels
numArrivals = 0, // Number of new tasks arriving
j; // Loop counter
// Seed the random number generator
srand((unsigned int)time(NULL));
// cout << endl << "Enter the number of priority levels : ";
// cin >> numPtyLevels;
cout << "Enter the length of time to run the simulator : ";
cin >> simLength;
time_t timer;
for (minute = 0; minute < simLength; minute++)
{
// Dequeue the first task in the queue (if any).
if (taskPQ.isEmpty() == false)
{
task=taskPQ.dequeue();
cout << "Priority " << task.priority << " task arrived at " << task.arrived << " & completed at " << minute << endl;
}
numArrivals = rand() % 4;
switch (numArrivals)
{
case 2:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
//task.priority = rand() % numPtyLevels - (task.arrived / simLength);
taskPQ.enqueue(task);
case 1:
task.arrived = minute;
task.priority = rand() % numPtyLevels;
taskPQ.enqueue(task);
default:
break;
}
}
return 0;
}
示例8: if
Vector<Vertex*> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
PriorityQueue<Vertex*> pqueue;
Vector<Vertex*> path;
pqueue.enqueue(start, 0);
start->cost = 0.0;
start->setColor(YELLOW);
while(!pqueue.isEmpty())
{
Vertex* v = pqueue.dequeue();
v->setColor(GREEN);
if (v == end)
break;
for (auto edge : v->edges)
{
Vertex* next_v = edge->finish;
if (next_v->getColor() == 0)
{
next_v->setColor(YELLOW);
double next_cost = v->cost + edge->cost;
next_v->previous = v;
next_v->cost = next_cost;
pqueue.enqueue(next_v, next_cost);
}
else if (next_v->getColor() == YELLOW)
{
double next_cost = v->cost + edge->cost;
if (next_cost < next_v->cost)
{
next_v->cost = next_cost;
next_v->previous = v;
pqueue.changePriority(next_v, next_v->cost);
}
}
}
}
if (end->getColor() == GREEN)
{
Vertex* path_v = end;
while (path_v->previous)
{
path.insert(0, path_v);
path_v = path_v->previous;
}
path.insert(0, path_v);
}
return path;
}
示例9: aStar
/*
* A* search. Will explore from 'end' until it finds 'start' or can determine that no valid path exists.
* It explores in reverse to make it easier to build path array.
*/
vector<Node *> aStar(BasicGraph& graph, Vertex* start, Vertex* end) {
graph.resetData();
vector<Vertex*> path;
PriorityQueue<Vertex*> openQueue;
//Setup starting state
end->cost = 0;
end->visited = true;
openQueue.enqueue(end, end->cost);
Vertex* current = nullptr;
//While there are still reachable nodes to explore
while(!openQueue.isEmpty()){
//Set current node to the next node in the queue
current = openQueue.dequeue();
current->setColor(GREEN);
//If current node is target, build path and return
if(current == start){
rewind(end, start, path);
return path;
}
//Add any unvisited neighbor to queue, adjust cost for already visited neighbor nodes
for(Vertex* neighbor : graph.getNeighbors(current)){
if(!neighbor->visited){
neighbor->previous = current;
neighbor->visited = true;
neighbor->cost = current->cost + graph.getArc(current, neighbor)->cost;
openQueue.enqueue(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}else{
double newCost = current->cost + graph.getArc(current, neighbor)->cost;
if(neighbor->cost > newCost){
neighbor->previous = current;
neighbor->cost = newCost;
openQueue.changePriority(neighbor, neighbor->cost + neighbor->heuristic(start));
neighbor->setColor(YELLOW);
}
}
}
}
return path;
}
示例10: kruskal
/*
* Method: kruskal
* Parameters: BasicGraph graph by reference
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Returns a set of Edges that correspond to the minimum
* spanning tree of the graph passed by reference. This Kruskal
* algorithm implementation leverages a Priority Queue to
* connect each Vertex with its minimum cost Edge without
* creating cycles, ensuring its a minimum spanning tree.
*/
Set<Edge*> kruskal(BasicGraph& graph) {
Set<Edge*> mst;
Map<Vertex*, Set<Vertex*> > clusters;
PriorityQueue<Edge*> pqueue;
initializeClusters(graph, clusters);
enqueueEdges(graph, pqueue);
while(!pqueue.isEmpty()) {
Edge* edge = pqueue.dequeue();
if(!clusters[edge->start].contains(edge->finish)) {
mergeClusters(clusters, edge->start, edge->finish);
mst.add(edge);
}
}
return mst;
}
示例11: find_path
void find_path(NavGridLocation from, NavGridLocation to)
{
int grid_size = gs.grid.get_width() * gs.grid.get_height();
int from_id = hash_location(from);
std::vector<AiNode> nodes (grid_size);
for (int i = 0; i < nodes.size(); i++) {
AiNode& node = nodes[i];
node.id = i;
node.visited = false;
node.prev_node = nullptr;
node.true_score = std::numeric_limits<float>::max();
node.guess_score = std::numeric_limits<float>::max();
node.pq_node = nullptr;
}
nodes[from_id].true_score = 0.f;
nodes[from_id].guess_score = heuristic_cost(unhash_location(from_id), to);
PriorityQueue<AiNode*> pq;
nodes[from_id].pq_node = pq.enqueue(&nodes[from_id], -nodes[from_id].guess_score);
update_prev(nodes);
while (!pq.is_empty()) {
AiNode* current = pq.get_front();
pq.dequeue();
current->pq_node = nullptr;
current->visited = true;
if (current->id == hash_location(to)) {
break;
}
for (const NavGridLocation& adj : gs.grid.get_adjacent(unhash_location(current->id))) {
AiNode* adj_node = &nodes[hash_location(adj)];
if (adj_node->visited)
continue;
float new_score = current->true_score + 1;
if (new_score >= adj_node->true_score)
continue;
adj_node->prev_node = current;
adj_node->true_score = new_score;
adj_node->guess_score = new_score + heuristic_cost(unhash_location(adj_node->id), to);
if (adj_node->pq_node) {
pq.update(adj_node->pq_node, -adj_node->guess_score);
} else {
adj_node->pq_node = pq.enqueue(adj_node, -adj_node->guess_score);
}
}
}
update_prev(nodes);
update_path(nodes, to);
}
示例12: main
int main()
{
//to be able to do fair benchmarks we better use a constant seed
srand(42);
PriorityQueue<int> pq;
for (int i = 1; i < 11; i++)
{
pq.enqueue(i);
}
pq.enqueue(1);
pq.enqueue(99);
pq.enqueue(2);
pq.enqueue(10);
pq.enqueue(3);
pq.enqueue(7);
while(!pq.empty())
{
cout << pq.front() << endl;
pq.dequeue();
}
return 0;
}
示例13: union_set
Set<Edge*> kruskal(BasicGraph& graph) {
Set<Vertex*> vertexs = graph.getVertexSet();
Set<Edge*> edges = graph.getEdgeSet();
map<Vertex*, int> vet_index;
PriorityQueue<Edge*> pqueue;
for (Edge* edge : edges)
pqueue.enqueue(edge, edge->cost);
int N = vertexs.size();
DisjointSet union_set(N);
int i = 0;
for (Vertex* vert : vertexs)
{
vet_index[vert] = i;
++i;
}
Set<Edge*> mst;
while (union_set.count() > 1)
{
Edge* edge = pqueue.dequeue();
int p = vet_index[edge->start];
int q = vet_index[edge->finish];
if (!union_set.connected(p, q))
{
union_set.connect(p, q);
mst.add(edge);
}
}
return mst;
}
示例14: main
int main()
{
PriorityQueue<DataType> testPQA;
PriorityQueue<DataType> testPQB;
PriorityQueue<DataType> testPQC;
char cmdChar;
DataType dataItem;
int qPriority;
char qProcess[ SMALL_STR_LEN ];
bool dataChanged;
ShowMenu();
do
{
dataChanged = false;
cout << endl << "Command: "; // Read command
cmdChar = GetCommandInput( qProcess, qPriority );
switch ( cmdChar )
{
case 'c': case 'C': // clear priority queue
while( !testPQA.isEmpty() )
{
testPQA.dequeue( dataItem );
}
if( VERBOSE )
{
cout << " Priority Queue has been cleared " << endl;
}
dataChanged = true;
break;
case 'd': case 'D': // dequeue one data item
testPQA.dequeue( dataItem );
if( VERBOSE )
{
cout << " Process: " << dataItem.process
<< " has been dequeued with a priority of "
<< dataItem.priority << PERIOD << endl;
}
dataChanged = true;
break;
case 'e': case 'E': // enqueue
testPQA.enqueue( qPriority, qProcess );
if( VERBOSE )
{
cout << " Process: " << qProcess
<< " has been enqueued with a priority of "
<< qPriority << PERIOD << endl;
}
dataChanged = true;
break;
case 'h': case 'H': // help request
ShowMenu();
break;
case 'p': case 'P': // peek at next item
testPQA.peekAtFront( dataItem );
if( VERBOSE )
{
cout << " Process: " << dataItem.process
<< " with priority: " << dataItem.priority
<< " found at front of queue." << endl;
}
break;
case 'q': case 'Q': // quit the test program
if( VERBOSE )
{
cout << " End Program Requested" << endl;
}
cout << endl;
break;
case 'x': case 'X': // create copy constructor PQ
//.........这里部分代码省略.........
示例15: main
int main()
{
cout << "\n\nLab 12b, PriorityQueueBigOh.cpp\n";
cout << "Programmer: Aysin Oruz \n";
cout << "Editor(s) used: JNotePad and Xcode \n";
cout << "Compiler(s) used: Xcode and Terminal \n";
cout << "Description: The purpose of this lab is for you to learn how to create and apply 12b, PriorityQueueBigOh and get values with BigO().\n";
cout << "File: " << __FILE__ << endl;
cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl;
const int REPS = 100000; // for timing fast operations, use REPS up to 100th of the starting n
int n = 10000000; // THE STARTING PROBLEM SIZE (MAX 250 MILLION)
string bigOh = "O(log n)"; // YOUR PREDICTION: O(1), O(log n), O(n), O(n log n), or O(n squared)
int elapsedTimeTicksNorm = 0;
double expectedTimeTicks = 0;
cout << "\nEnqueue() O(log n)\n" << endl;
for (int cycle = 0; cycle < 4; cycle++, n*= 2)
{
//Declare PQ
PriorityQueue<int> List;
//Go thru loop to enter values
for (int i = n; i > 0; i--)
List.enqueue(i);
clock_t startTime = clock(); // start the timer
assert(List.getSize() == n);
for(int j = 0; j < REPS; j++ )
List.enqueue(n + j);
assert(List.getSize() == n + REPS);
clock_t endTime = clock(); //stop time
for (int i = 0; i < List.getSize(); i++)
{
int first = List.dequeue();
int second = List.dequeue();
assert(first >= second);
}
// compute timing results
long elapsedTimeTicks = (long)(endTime - startTime);
double factor = pow(2.0, cycle);
if (cycle == 0)
elapsedTimeTicksNorm = elapsedTimeTicks;
else if (bigOh == "O(1)")
expectedTimeTicks = elapsedTimeTicksNorm;
else if (bigOh == "O(log n)")
expectedTimeTicks = log(double(n)) / log(n / factor) * elapsedTimeTicksNorm;
else if (bigOh == "O(n)")
expectedTimeTicks = factor * elapsedTimeTicksNorm;
else if (bigOh == "O(n log n)")
expectedTimeTicks = factor * log(double(n)) / log(n / factor) * elapsedTimeTicksNorm;
else if (bigOh == "O(n squared)")
expectedTimeTicks = factor * factor * elapsedTimeTicksNorm;
cout << elapsedTimeTicks;;
if (cycle == 0) cout << " (expected " << bigOh << ')';
else cout << " (expected " << expectedTimeTicks << ')';
cout << " for n = " << n << endl;
}
{
const int REPS = 10000; // for timing fast operations, use REPS up to 100th of the starting n
int n = 1000000; // THE STARTING PROBLEM SIZE (MAX 250 MILLION)
cout << "\nDequeue() O(log n)\n" << endl;
for (int cycle = 0; cycle < 4; cycle++, n*= 2)
{
PriorityQueue<int> List;
for(int i = n; i > 0; i--)
List.enqueue(i);
assert(List.getSize() == n);
//start timing
clock_t startTime = clock();
for(int j = 0; j < REPS; j++)
List.dequeue();
clock_t endTime = clock(); //stop timign
assert(List.getSize() == n - REPS);
for (int i = 0; i < List.getSize(); i++)
{
int first = List.dequeue();
int second = List.dequeue();
assert(first >= second);
}
// compute timing results
long elapsedTimeTicks = (long)(endTime - startTime);
double factor = pow(2.0, cycle);
if (cycle == 0)
elapsedTimeTicksNorm = elapsedTimeTicks;
else if (bigOh == "O(1)")
expectedTimeTicks = elapsedTimeTicksNorm;
//.........这里部分代码省略.........