1. 程式人生 > >資料結構基礎及STL實現(複習)

資料結構基礎及STL實現(複習)

目錄

動態陣列

    srand(time(0));
    std::vector<int> qwq;
    
    for(int i = 1;i <= 10;++i)
        qwq.push_back(rand()%100);
    
    int n = qwq.size();
    
    for(int i = 1;i <= n;++i)
        std::cout<< qwq[i-1] << " "; 

    std::cout << "\n";
    
    sort(qwq.begin(),qwq.end());
    
    for(int i = 1;i <= n;++i)
        std::cout<< qwq[i-1] << " "; 

    srand(time(0));
    std::stack<int> qwq;
    
    for(int i = 1;i <= 10;++i)
    {
        int x = rand()%100;
        std::cout << x << " ";
        qwq.push(x);
    }
    
    std::cout << "\n";
    int n = qwq.size();
    
    for(int i = 1;i <= n;++i)
    {
        std:: cout << qwq.top() <<" ";
        qwq.pop();
    }

佇列

    srand(time(0));
    std::queue<int> qwq;
    
    for(int i = 1;i <= 10;++i)
        qwq.push(rand()%100);

    int n = qwq.size();
    
    for(int i = 1;i <= n;++i)
    {
        std::cout << qwq.front() << " ";
        qwq.pop();
    }
    
    if(qwq.empty()) std::cout << "\n佇列已經清空!";

優先佇列

//1. 大根堆
    srand(time(0));
    std::priority_queue<int> qwq;
    
    for(int i = 1;i <= 10;++i)
        qwq.push(rand()%100);
    
    int n = qwq.size();
    
    for(int i = 1;i <= n;++i)
    {
        std::cout << qwq.top() << " ";
        qwq.pop();
    }
    
    std::cout << std::endl; 
//2. 小根堆
    std::priority_queue<int,std::vector<int>,std::greater<int> > qaq;
    
    for(int i = 1;i <= 10;++i)
        qaq.push(rand()%100);
    
    n = qaq.size();
    
    for(int i = 1;i <= n;++i)
    {
        std::cout << qaq.top() << " ";
        qaq.pop();
    }