本文整理汇总了C++中PriorityQueue::back方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::back方法的具体用法?C++ PriorityQueue::back怎么用?C++ PriorityQueue::back使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::back方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
stack.pop();
cout<<stack.pop()<<endl;
cout<<"\n\n";
cout<<"testing copy construcor\n";
Stack<int> newStack(stack);
cout<<"stack top(): "<<stack.top()<<endl;
cout<<"stack size(): "<<stack.size()<<endl;
cout<<"newStack top(): "<<newStack.top()<<endl;
cout<<"newStack size(): "<<newStack.size()<<endl;
cout<<"\n\n";
// cout<<"Clearing newStack\n";
// newStack.clear();
// cout<<"stack size(): "<<stack.size()<<endl;
// cout<<"stack top(): "<<stack.top()<<endl;
// cout<<"newStack size(): "<<newStack.size()<<endl;//outputs 0
// cout<<"newStack top(): "<<newStack.top()<<endl;//causes subscript error
// cout<<"\n\n";
Queue<int> queue(0);
cout << "Populating queue"<<endl;
for (int var = 0; var < 10; ++var) {
queue.pushBack(var+1);
}
cout<<"Queue Size: "<<queue.size()<<endl;
cout<<"\n\n";
cout<<"Testing front() and back() functions\n";
cout<<queue.front()<<endl;
cout<<queue.back()<<endl;
cout<<"\n\n";
cout<<"Testing PopFront() function\n";
cout<<queue.popFront()<<endl;
cout<<"Queue Size: "<<queue.size()<<endl;
cout<<"\n\n";
cout<<"testing copy construcor\n";
Queue<int> newQueue(queue);
cout<<"queue front(): "<<queue.front()<<endl;
cout<<"queue size(): "<<queue.size()<<endl;
cout<<"newQueue front(): "<<newQueue.front()<<endl;
cout<<"newQueue size(): "<<newQueue.size()<<endl;
cout<<"\n\n";
cout<<"Popping Valued from newQueue\n";
for (int i = 0; i < 5; ++i) {
cout<<newQueue.popFront()<<endl;
}
cout<<"queue front(): "<<queue.front()<<endl;
cout<<"queue size(): "<<queue.size()<<endl;
cout<<"newQueue front(): "<<newQueue.front()<<endl;
cout<<"newQueue size(): "<<newQueue.size()<<endl;
cout<<"\n\n";
cout<<"***********************************\nTesting Priority Linked List\n";
P_LinkedList<int> pll;
示例2: main
int main()
{
// print my name and this assignment's title
cout << "Lab 11a, The \"Write And Test A Priority Queue Template\" Program \n";
cout << "Programmer: Licong Wang\n";
cout << "Editor(s) used: Visual studio 2013\n";
cout << "Compiler(s) used: Microsoft c++ complier\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
PriorityQueue<int> pq;
cout << "\nTest size and empty" << endl;
cout << "The expected size is 0, the acutal size is " << pq.size() << endl;
assert(pq.size() == 0);
cout << "Expect it to be empty: ";
if (pq.empty())
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
assert(pq.empty());
cout << "\nEnqueue values 1 to 5" << endl;
for (int i = 1; i < 6; i++)
{
pq.enqueue(i);
}
cout << "\nTest size and empty again" << endl;
cout << "The expected size is 5, the acutal size is " << pq.size() << endl;
assert(pq.size() == 5);
cout << "Expect it to be not empty: ";
if (pq.empty())
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
assert(!(pq.empty()));
cout << "\nCheck its front and back, expecting 5 and 3, accroding to the rule of binary tree" << endl;
cout << "Front: " << pq.front() << endl;
cout << "back: " << pq.back() << endl;
assert(pq.front() == 5);
assert(pq.back() == 3);
cout << "\nCheck the heap array using copy pop, expecting values from 5-1, "
<< "since the highest value is popped each time" << endl;
for (PriorityQueue<int> copy = pq; copy.size(); )
cout << copy.dequeue() << " ";
cout << "\n\nNow dequeue once" << endl;
pq.dequeue();
cout << "\nTest size again" << endl;
cout << "The expected size is 4, the acutal size is " << pq.size() << endl;
assert(pq.size() == 4);
cout << "\nCheck its front and back, expecting 4 and 1" << endl;
cout << "Front: " << pq.front() << endl;
cout << "back: " << pq.back() << endl;
assert(pq.front() == 4);
assert(pq.back() == 1);
cout << "\ncheck the heap array using copy pop, expecting values from 4-1, "
<< "since 5 is already dequeued" << endl;
for (PriorityQueue<int> copy = pq; copy.size(); )
cout << copy.dequeue() << " ";
cout << endl;
{
cout << "\nObject Copy Testing" << endl;
const PriorityQueue<int> copy = pq;
cout << "\nTest size and empty" << endl;
cout << "The expected size is 4, the acutal size is " << pq.size() << endl;
assert(pq.size() == 4);
cout << "Expect it to be not empty: ";
if (pq.empty())
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
assert(!(pq.empty()));
cout << "\ncheck the heap array using copy pop, expecting values from 4-1" << endl;
for (PriorityQueue<int> copy2 = copy; copy2.size(); )
cout << copy2.dequeue() << " ";
cout << endl;
}
{
cout << "\nObject Assignment Testing" << endl;
PriorityQueue<int> copy; copy = pq;
cout << "\nTest size and empty" << endl;
cout << "The expected size is 4, the acutal size is " << pq.size() << endl;
assert(pq.size() == 4);
cout << "Expect it to be not empty: ";
if (pq.empty())
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
//.........这里部分代码省略.........
示例3: testPriorityQueue
void testPriorityQueue(){
PriorityQueue<int> mycontainer;
cout << "\n\n Begin test function for the PriorityQueue<T> class\n";
// Testing the enqueue function
cout << "Testing size of new empty container: " << mycontainer.length() << endl;
cout << "Testing enqueue(1), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(1);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
mycontainer.enqueue(2);
cout << "Size is " << mycontainer.length() << endl;
cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl << endl;
int size = mycontainer.length();
cout << "Testing pop_back(), front(), back(), length() and isEmpty() functions \n"
<< "in a for loop with iterations greater than container size.";
for (int i = 0; i < size + 1; i++) {
cout << "\nIteration: " << i + 1 << "\n";
if (!mycontainer.isEmpty()) {
cout << " mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "PriorityQueue size before pop is " << mycontainer.length() << endl;
//cout<<"Front of container is " << mycontainer.front()<<endl;
//cout<<"Back of container is " << mycontainer.back()<<endl;
//cout << "Popping: " << mycontainer.front() << endl << endl;
mycontainer.pop_back();
} else {
cout << "The PriorityQueue is empty.\n";
}
cout << "PriorityQueue size is now: " << mycontainer.length() << endl;
}
cout << "\nFinished for loop\n";
cout << "\nTesting the reference for front() and back() functions.\n";
cout << "Start with int test = 7. mycontainer.enqueue(test)\n";
int test = 7;
mycontainer.enqueue(test);
cout << "Testing with test = 8. test=mycontainer.front(). mycontainer.front() = 13 \n";
test = 8;
test = mycontainer.front();
mycontainer.front() = 13;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
test = 11;
mycontainer.enqueue(test);
cout << "Back of container is: " << mycontainer.back() << endl;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
mycontainer.back() = test;
cout << "Test is now " << test << " front of container is " << mycontainer.front()
<< " back of container is " << mycontainer.back() << endl;
cout << "mycontainer size is " << mycontainer.length() << endl;
cout << "\nTesting the clear() function: \n";
mycontainer.clear();
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "\nTesting assignment operator: container2 = mycontainer\n";
cout << "Filling mycontainer with ints starting at 42\n";
size = 5;
// Fill mycontainer with ints to test copy constructor
for (int i = 0; i < size; i++) {
mycontainer.enqueue(i + 41);
}
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
PriorityQueue<int> container2;
container2 = mycontainer;
cout << "mycontainer size is: " << mycontainer.length() << endl;
cout << "container2 size is: " << container2.length() << endl;
cout << "Testing the contents of container2 and mycontainer using back() and pop_back() functions:\n";
size = container2.length();
for (int i = 0; i < size; i++) {
cout << "Attempting front and pop functions. Iteration: " << i + 1 << "\n";
//.........这里部分代码省略.........
示例4: main
int main()
{
// print my name and this assignment's title
cout << "LAB 11a: Write And Test A Priority Queue Template\n";
cout << "Programmer: Jacky Chow\n";
cout << "Editor(s) used: Notepad++\n";
cout << "Compiler(s) used: Visual C++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
PriorityQueue<int> pq;
int temp;
cout << "Created a PriorityQueue<int> named pq\n";
cout << "Its size should be 0. It is: " << pq.size() << endl;
assert(0 == pq.size());
if(pq.empty()) cout << "pq.empty() returned true that it was empty\n";
else cout << "Error: pq.empty() returned false for empty PQ\n";
assert(pq.empty());
cout << "\nEnqueuing the ints 13, 8, 4, 20, 10 into pq\n";
pq.enqueue(13);
pq.enqueue(8);
pq.enqueue(4);
pq.enqueue(20);
pq.enqueue(10);
if(!pq.empty()) cout << "pq.empty() returned false that it was empty\n";
else cout << "Error: pq.empty() returned true for a non-empty PQ\n";
assert(!pq.empty());
cout << "\nIts size should now be 5. It is: " << pq.size() << endl;
assert(5 == pq.size());
cout << "The front should be 20. It is: " << pq.front() << endl;
assert(20 == pq.front());
cout << "The back should be 10. It is: " << pq.back() << endl;
assert(10 == pq.back());
//const copy constructor test
{
cout << "\nCreating const copy with copy constructor\n";
const PriorityQueue<int> copy = pq;
if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n";
else cout << "Error: copy.empty() returned true for a non-empty PQ\n";
assert(!copy.empty());
cout << "Copy size should now be 5. It is: " << copy.size() << endl;
assert(5 == copy.size());
}
//operator= copy test
{
cout << "\nCreating copy with with operator=\n";
PriorityQueue<int> copy;
cout << "Should initially have size 0. It is: " << copy.size() << endl;
assert(copy.empty());
cout << "Assigning copy to = pq\n";
copy = pq;
if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n";
else cout << "Error: copy.empty() returned true for a non-empty copy\n";
assert(!copy.empty());
cout << "Copy 2's size should now be 5. It is: " << copy.size() << endl;
assert(5 == copy.size());
cout << "The front should be 20. It is: " << copy.front() << endl;
assert(20 == copy.front());
cout << "The back should be 10. It is: " << copy.back() << endl;
assert(10 == copy.back());
cout << "Dequeuing the entire copy of pq: \n";
for(; copy.size();)
cout << copy.dequeue() << ' ';
cout << "\nCopy should now be size 0. It is: " << copy.size() << endl;
assert(copy.empty());
if(copy.empty()) cout << "copy.empty() returned true that it was empty\n";
else cout << "Error: copy.empty() returned false for an empty copy\n";
assert(copy.empty());
}
temp = pq.dequeue();
cout << "\nDequeuing root of pq. It should return 20. It is: " << temp << endl;
assert(20 == temp);
cout << "\nIts size should now be 4. It is: " << pq.size() << endl;
assert(4 == pq.size());
cout << "The front should be 13. It is: " << pq.front() << endl;
assert(13 == pq.front());
cout << "The back should be 8. It is: " << pq.back() << endl;
assert(8 == pq.back());
cout << "\nNow using clear to clear pq\n";
pq.clear();
cout << "Size should now be 0. It is: " << pq.size() << endl;
assert(0 == pq.size());
if(pq.empty()) cout << "pq.empty() returned true that it was empty\n";
else cout << "Error: pq.empty() returned false for empty PQ\n";
assert(pq.empty());
}