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


C++ Heap::add方法代码示例

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


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

示例1:

TEST(HeapTest, SimpleHeapTest)
{
    Heap<int> heap;

    EXPECT_TRUE(heap.isEmpty());

    heap.add(1);
    heap.add(2);
    heap.add(3);

    EXPECT_EQ(heap.getNumNodes(), 3);
    EXPECT_EQ(heap.getHeight(), 2);

    for (int i = 4; i <= 15; i++)
    {
        heap.add(i);
    }

    ASSERT_EQ(heap.getNumNodes(), 15);
    EXPECT_EQ(heap.getHeight(), 4);

    int* testArray = new int[15];
    for (int i = 0; i < 15; i++)
    {
        testArray[i] = heap.peekTop();
        heap.remove();
    }

    for (int i = 0; i < 14; i++)
    {
        EXPECT_GE(testArray[i], testArray[i + 1]);
    }

    heap.clear();

    EXPECT_TRUE(heap.isEmpty());

    delete[] testArray;
}
开发者ID:kevinkyang,项目名称:DataStructures,代码行数:39,代码来源:HeapTest.cpp

示例2: main

int main()
{
    Heap hp;
    hp.positions[1] = -1;
    hp.sizze = 0;
    int m,n,a,b,edges;
    cin>>n>>m;
    edges = m;
    while(m--)
    {
        cin>>a>>b;
        if(a!=b)
        {
            nodes[a].push_back(make_pair(b,0));
            nodes[b].push_back(make_pair(a,1));
        }
    }
    for(int i=2;i<=n;i++)
    {
        distances[i] = MYMAX;
        hp.add(make_pair(i,MYMAX));
    }
    distances[1] = 0;
    for(int i=0;i<nodes[1].size();i++)
        hp.update(nodes[1][i].first,nodes[1][i].second);

    while(!hp.isEmpty())
    {
        pair<int,int> minEle = hp.removeMin();
        distances[minEle.first] = minEle.second;
        for(int i=0;i<nodes[minEle.first].size();i++)
            hp.update(nodes[minEle.first][i].first, distances[minEle.first] + nodes[minEle.first][i].second);
    }

    if(0<=distances[n] && distances[n]<=edges)
        cout<<distances[n];
    else
        cout<< -1;
    return 0;
}
开发者ID:shyamelc,项目名称:Coding-PC,代码行数:40,代码来源:main.cpp

示例3: heapTests

void heapTests(Heap &h, int n) {
	clock_t start, stop;
	cout << "Adding " << n << " elements...";
	cout.flush();
	start = clock();
	for (int i = 0; i < n; i++) {
		h.add(rand()%100);
	}
	stop = clock();
	cout << "done (" << ((double)(stop-start))/CLOCKS_PER_SEC << "s)" << endl;

	cout << "Removing " << n << " elements...";
	cout.flush();
	start = clock();
	int p = h.remove();
	for (int i = 1; i < n; i++) {
		assert(h.size() == n-i);
		int q = h.remove();
		assert(p <= q);
		p = q;
	}
	stop = clock();
	cout << "done (" << ((double)(stop-start))/CLOCKS_PER_SEC << "s)" << endl;
}
开发者ID:Coleman-Creative,项目名称:ods,代码行数:24,代码来源:main.cpp

示例4: testHeap

void testHeap(){
  Heap<int,std::string> myHeap;
  myHeap.add(std::make_pair(5,"5"));
  myHeap.add(std::make_pair(1,"1"));
  myHeap.add(std::make_pair(0,"0"));
  myHeap.add(std::make_pair(7,"7"));
  myHeap.add(std::make_pair(10,"10"));
  
  if(myHeap.getNumItems() != 5){
    std::cout << "ERROR: Expected 5 items, but got " << myHeap.getNumItems() << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found 5 items, as expected." << std::endl;
  }

  std::pair<int,std::string> item;
  item = myHeap.remove();
  if(item.first != 0){
    std::cout << "ERROR: Expected key 0, but got " << item.first << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found item 0 as expected." << std::endl;
  }

  if(myHeap.getNumItems() != 4){
    std::cout << "ERROR: Expected 4 items, but got " << myHeap.getNumItems() << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found 4 items, as expected." << std::endl;
  }

  item = myHeap.remove();
  if(item.first != 1){
    std::cout << "ERROR: Expected key 1, but got " << item.first << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found item 1 as expected." << std::endl;
  }

  item = myHeap.remove();
  if(item.first != 5){
    std::cout << "ERROR: Expected key 5, but got " << item.first << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found item 5 as expected." << std::endl;
  }

  item = myHeap.remove();
  if(item.first != 7){
    std::cout << "ERROR: Expected key 7, but got " << item.first << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found item 7 as expected." << std::endl;
  }

  item = myHeap.remove();
  if(item.first != 10){
    std::cout << "ERROR: Expected key 10, but got " << item.first << std::endl;
    exit(1);
  } else {
    std::cout << "SUCCESS: Found item 10 as expected." << std::endl;
  }

}
开发者ID:kosirjm,项目名称:Portfolio,代码行数:64,代码来源:main.cpp

示例5: main

int main()
{
	int tp,qn,iq,n,i,j,cstout=0;
	float t,totaltime,currentime,pos,timed=0;
	event temp;
	customer* tcst=new customer;
	customer* entered=NULL;
	cin>>totaltime;
	currentime=0;
	cin>>qn;
	cin>>iq;
	queue q[qn];
	Heap a;
	for (i=0;i<qn-1;i++)
	{
		cin>>q[i].lambda;
	}	
	for (i=0;i<qn-1;i++)
	{
		cin>>tp;
		q[i].out=tp;
		for (j=0;j<tp;j++)
		{
			cin>>q[i].q[j];
			cin>>q[i].pro[j];
			if (j>0)
			q[i].pro[j]=q[i].pro[j]+pos;
			pos=q[i].pro[j];
		}
	}
	pos=0;
	for (i=0;i<iq;i++)
	{
		t=currentime+gen_lam(q[i].lambda);
		a.add(t,i);
	}
	temp=a.del();
	currentime=temp.t;
	while (currentime<totaltime)
	{
		if (temp.qno<iq)
		{
			pos=gen_r();
			j=0;
			while (j<q[temp.qno].out)
			{
				if (pos<q[temp.qno].pro[j])
				break;
				j++;
			}
			if (j==q[temp.qno].out) j--;
			tp=q[temp.qno].q[j];
			customer* newcst=new customer;
			newcst->in_time=currentime;
			if (entered!=NULL) 
			{
				entered->next=newcst;
				newcst->pre=entered;
			}
			entered=newcst;
			if (q[tp].pre==0)
			{
				q[tp].enqueue(newcst);
				t=currentime+gen_lam(q[tp].lambda);
				a.add(t,tp);
				
			}
			else
			{
				q[tp].enqueue(newcst);
			}
			t=currentime+gen_lam(q[temp.qno].lambda);
			a.add(t,temp.qno);
		}
		else
		{
			tcst=q[temp.qno].dequeue();
			if (q[temp.qno].pre)          
			{
				t=currentime+gen_lam(q[temp.qno].lambda);
				a.add(t,temp.qno);
			}
			pos=gen_r();
			j=0;
			while (j<q[temp.qno].out)
			{
				if (pos<q[temp.qno].pro[j])
				break;
				j++;
			}
			if (j==q[temp.qno].out) j--;
			tp=q[temp.qno].q[j];
			if (tp==qn-1)
			{
				if (tcst->pre!=NULL&&tcst->next!=NULL)
				{
					tcst->pre->next=tcst->next;
					tcst->next->pre=tcst->pre;
				}
				else if (tcst->pre==NULL&&tcst->next!=NULL)
//.........这里部分代码省略.........
开发者ID:Bytelandian,项目名称:college-assignments,代码行数:101,代码来源:queueing.cpp


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