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


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

本文整理汇总了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();
}
开发者ID:bgold09,项目名称:psu_cmpsc,代码行数:52,代码来源:main.cpp

示例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;
        }
开发者ID:mothaibatacungmua,项目名称:Algorithms-C-Plus,代码行数:49,代码来源:prim.cpp

示例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();
}
开发者ID:hanxiaojing,项目名称:PriorityQueue,代码行数:17,代码来源:PriorityQueuetest.cpp

示例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;
}
开发者ID:farnyser,项目名称:pg,代码行数:74,代码来源:main.cpp


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