本文整理汇总了C++中PQ::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ PQ::insert方法的具体用法?C++ PQ::insert怎么用?C++ PQ::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PQ
的用法示例。
在下文中一共展示了PQ::insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testHeap
void testHeap ( int n ) {
T* A = new T[2*n/3]; //创建容量为2*n/3的数组,并
for ( int i = 0; i < 2 * n / 3; i++ ) A[i] = dice ( ( T ) 3 * n ); //在其中随机生成2*n/3个词条
/*DSA*/printf ( "%d random keys created:\n", 2 * n / 3 );
/*DSA*/for ( int i = 0; i < 2 * n / 3; i++ ) print ( A[i] ); printf ( "\n" );
PQ heap ( A + n / 6, n / 3 ); //批量建堆(PQ_ComplHeap实现了Robert Floyd算法)
delete [] A;
/*DSA*/system("cls"); print ( heap ); Sleep(100);
while ( heap.size() < n ) { //随机测试
if ( dice ( 100 ) < 70 ) { //70%概率插入新词条
T e = dice ( ( T ) 3 * n ); /*DSA*/printf ( "Inserting" ); print ( e ); printf ( " ...\n" );
heap.insert ( e ); /*DSA*/printf ( "Insertion done\n" );
} else { //30%概率摘除最大词条
if ( !heap.empty() ) {
/*DSA*/printf ( "Deleting max ...\n" );
T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
}
}
/*DSA*/system("cls"); print ( heap ); Sleep(100);
}
while ( !heap.empty() ) { //清空
T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" );
/*DSA*/system("cls"); print ( heap ); Sleep(100);
}
}
示例2: insertNext
//Insert limit elements of the file fin into heap.
void insertNext(PQ & pq, ifstream & fin, int limit = 0)
{
if (limit == 0)
limit = numeric_limits<int>::max();
string word;
int ct;
while (!fin.eof() && pq.size < limit){
fin >> word >> ct;
pq.insert(ItemType(word, ct));
}
}