当前位置: 首页>>代码示例>>C++>>正文


C++ PriorityQueue::clear方法代码示例

本文整理汇总了C++中PriorityQueue::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::clear方法的具体用法?C++ PriorityQueue::clear怎么用?C++ PriorityQueue::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue::clear方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
    // push , pop , top test
    {
        PriorityQueue<int> p;
        assert(p.getSize() == 0);
        assert(p.getCapacity() == 21);

        p.push(3);p.push(2);p.push(7);p.push(11);p.push(23);
        p.pop();p.pop();
        p.push(3);p.push(2);p.push(43);p.push(21);p.push(25);
        p.pop();p.pop();
        vector<int> vcmp = {7, 11, 21, 23, 25, 43};

        assert(p.getSize() == 6);
        assert( vcmp == print_queue<int>(p));
        assert(p.getCapacity() == 21);

    }

    // size , capacity , clear , destroy test
    {
        PriorityQueue<int> p;
        assert(p.getSize() == 0);
        assert(p.getCapacity() == 21);

        for(auto i = 0; i<21; i++){
            p.push(i);
        }
        assert(p.getSize() == 21);
        assert(p.getCapacity() == 42);

        p.clear();
        assert(p.getSize() == 0);
        assert(p.getCapacity() == 42);

        p.destroy();
        assert(p.getSize() == 0);
        assert(p.getCapacity() == 0);
    }

    // heapsort test
    {
        vector<int> v = {1, 16, 12, 2, 25, 5, 6};
        vector<int> cmpv = {1, 2, 5, 6, 12, 16, 25};
        heap_sort(v.begin(), v.end());
        assert(v == cmpv);
        
    }


    cout<< "All TestCases Pass."<<endl;
    return 0;
}
开发者ID:arctanx0,项目名称:Swag-Libraries,代码行数:54,代码来源:heap.cpp

示例2: mst

void Prim::mst(int root,Color color){
	Q.clear();
	int size=G->V();  // number of vertices
	for(int i=0;i<size;i++){
		dist[i]=1000.0;
		edges[i]=NO_EDGE;
		if(G->get_node_value(i)==color){  // only choose the same color
		    	//dist[i]=INF;    // equal to infinite
			//cout<<"insert node!"<<i<<" and dist is "<<dist[i]<<endl;
		Q.Insert(Type_Queue_Element (i,dist[i]));  // assign V[G] to Q
		}
	}
	dist[root]=0.0;
	if(!Q.contains(root)) cout<<"not include root !!"<<endl;
	else {
		Q.chgPrioirity(root,dist[root]);   //dist[i] and priority value in priority queue must be synchronized
		edges[root]=ROOT_EDGE;
		while(!Q.empty()){
			Type_Queue_Element currElement=Q.top();
            Q.minPrioirty();  // remove from priority queue
			int currNode=currElement.first;
			if(edges[currNode]!=NO_EDGE){
				dist[currNode]=currElement.second;
				vector<int> neibs=G->neighbors(currNode,color);
				for(unsigned int i=0;i<neibs.size();i++){
					if(Q.contains(neibs[i]) && (G->get_edge_value(neibs[i],currNode)<dist[neibs[i]]) ){
						edges[neibs[i]]=currNode;
						dist[neibs[i]]=G->get_edge_value(neibs[i],currNode);
						Q.chgPrioirity(neibs[i],dist[neibs[i]]);
					}
				}
			}

		}
	}
}
开发者ID:ktjones,项目名称:CPP4C_Programmers,代码行数:36,代码来源:HWFive.cpp

示例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";

//.........这里部分代码省略.........
开发者ID:Program0,项目名称:ZerothMarlo_CSC17C,代码行数:101,代码来源:main.cpp

示例4: main

int main(int argc, char * argv[])
{
  using namespace std;
  using namespace Eigen;
  using namespace igl;
  cout<<"Usage: ./703_Decimation_bin [filename.(off|obj|ply)]"<<endl;
  cout<<"  [space]  toggle animation."<<endl;
  cout<<"  'r'  reset."<<endl;
  // Load a closed manifold mesh
  string filename(TUTORIAL_SHARED_PATH "/fertility.off");
  if(argc>=2)
  {
    filename = argv[1];
  }
  MatrixXd V,OV;
  MatrixXi F,OF;
  read_triangle_mesh(filename,OV,OF);

  igl::viewer::Viewer viewer;

  // Prepare array-based edge data structures and priority queue
  VectorXi EMAP;
  MatrixXi E,EF,EI;
  typedef std::set<std::pair<double,int> > PriorityQueue;
  PriorityQueue Q;
  std::vector<PriorityQueue::iterator > Qit;
  // If an edge were collapsed, we'd collapse it to these points:
  MatrixXd C;
  int num_collapsed;

  // Function to reset original mesh and data structures
  const auto & reset = [&]()
  {
    F = OF;
    V = OV;
    edge_flaps(F,E,EMAP,EF,EI);
    Qit.resize(E.rows());

    C.resize(E.rows(),V.cols());
    VectorXd costs(E.rows());
    Q.clear();
    for(int e = 0;e<E.rows();e++)
    {
      double cost = e;
      RowVectorXd p(1,3);
      shortest_edge_and_midpoint(e,V,F,E,EMAP,EF,EI,cost,p);
      C.row(e) = p;
      Qit[e] = Q.insert(std::pair<double,int>(cost,e)).first;
    }
    num_collapsed = 0;
    viewer.data.clear();
    viewer.data.set_mesh(V,F);
    viewer.data.set_face_based(true);
  };

  const auto &pre_draw = [&](igl::viewer::Viewer & viewer)->bool
  {
    // If animating then collapse 10% of edges
    if(viewer.core.is_animating && !Q.empty())
    {
      bool something_collapsed = false;
      // collapse edge
      const int max_iter = std::ceil(0.01*Q.size());
      for(int j = 0;j<max_iter;j++)
      {
        if(!collapse_edge(
          shortest_edge_and_midpoint, V,F,E,EMAP,EF,EI,Q,Qit,C))
        {
          break;
        }
        something_collapsed = true;
        num_collapsed++;
      }

      if(something_collapsed)
      {
        viewer.data.clear();
        viewer.data.set_mesh(V,F);
        viewer.data.set_face_based(true);
      }
    }
    return false;
  };

  const auto &key_down =
    [&](igl::viewer::Viewer &viewer,unsigned char key,int mod)->bool
  {
    switch(key)
    {
      case ' ':
        viewer.core.is_animating ^= 1;
        break;
      case 'R':
      case 'r':
        reset();
        break;
      default:
        return false;
    }
    return true;
//.........这里部分代码省略.........
开发者ID:nclement,项目名称:libigl,代码行数:101,代码来源:main.cpp

示例5: main


//.........这里部分代码省略.........
  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;
    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 << "\nBack to original object, test clear" << endl;
  pq.clear();

  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 << "\nCheck its front and back, expecting 0 and 0, dummy got returned" << endl;
  cout << "Front: " << pq.front() << endl;
  cout << "back: " << pq.back() << endl;
  assert(pq.front() == 0);
  assert(pq.back() == 0);

  cout << "\nPress ENTER to continue..." << endl;
  cin.get();
}
开发者ID:simida1234321,项目名称:TestOnly,代码行数:101,代码来源:PriorityQueueDriver.cpp

示例6: main

/* main() manages the user interface;
 * instantiates priority queue object, then operates loop to read input 
 * from user and call the appropriate priority queue method
 */
int main() {
   PriorityQueue pq;
   TokenScanner scanner;
   while (true) {
      string line = getLine("> ");
      scanner.setInput(line);
      scanner.ignoreWhitespace();
      string cmd=scanner.nextToken();
      if (cmd == "help") {
         helpCommand();
      }      
      else if (cmd == "enqueue") {
	if(scanner.hasMoreTokens()){
	  string value=scanner.nextToken();
	  if(scanner.hasMoreTokens()){
	    scanner.scanNumbers();
	    string priorityStr=scanner.nextToken();
	    double priority=stringToDouble(priorityStr);	
	    pq.enqueue(value,priority);
	  }
	  else
	    pq.enqueue(value);
	}
      }    
      else if (cmd == "dequeue") {
	if(pq.isEmpty())
	  cout<<"The queue is empty"<<endl;
	else
	  cout<<pq.dequeue()<<endl;
      }
      else if (cmd == "peek") {
	if(pq.isEmpty())
	  cout<<"The queue is empty"<<endl;
	else
	  cout<<pq.peek()<<endl;
      }
      else if (cmd == "peekPriority"||cmd=="peekpriority") {
	if(pq.isEmpty())
	  cout<<"The queue is empty"<<endl;
	else
	  cout<<pq.peekPriority()<<endl;
      }
      else if (cmd == "clear") {
	pq.clear();
      }
      else if (cmd == "size") {
	cout<<pq.size()<<endl;
      }
      else if (cmd == "isEmpty"||cmd=="isempty") {
	if(pq.isEmpty())
	  cout<<"true";
	else
	  cout<<"false";
	cout<<endl;
      }
      else if(cmd=="list")
	list(pq);
      else {
         cout << "Undefined command: " << cmd << endl;
      }
   }
   return 0;
}
开发者ID:jwhurt,项目名称:Projects,代码行数:67,代码来源:PQTest.cpp

示例7: 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());  
}
开发者ID:jchow751,项目名称:COMSC-210,代码行数:92,代码来源:PriorityQueueDriver.cpp


注:本文中的PriorityQueue::clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。