C++杂记1

C++中用rand来生成随机数,顺便复习优先队列的实现方式

实例代码:

int main()
{

    srand( time(NULL) );
    //用最大堆的形式实现
    priority_queue<int> pq;
    //用最小堆的形式实现
    priority_queue<int, vector<int>, greater<int> > pq2;

    for(int i = 0; i < 10; i++){
        int num = rand()%100;
        pq.push(num);
        pq2.push(num);
    }

    while( !pq.empty() ){
        cout << pq.top() << " ";
        pq.pop();
    }

    cout << endl;
    while( !pq2.empty() ){
        cout << pq2.top() << " ";
        pq2.pop();
    }


}

会出现报错 srand was not declared in this scope
这时候需要我们导进去stdlib.h 这个头文件

猜你喜欢

转载自blog.csdn.net/qq_37982109/article/details/88640496