4-ma’ruza mavzu: konteynerlarning adapterlari. Reja


Ustuvor navbat (Priority_queue)


Download 0.66 Mb.
bet3/9
Sana22.11.2023
Hajmi0.66 Mb.
#1794282
1   2   3   4   5   6   7   8   9
Bog'liq
4-ma’ruza mavzu konteynerlarning adapterlari. Reja

Ustuvor navbat (Priority_queue). Ustuvor navbat (Priority_queue) bilan navbat (queue) - oddiy navbatdek, lekin ustuvor navbatga yangi element qo‘shilasa, shunda navbat kamayish tartibida saralanadi. Shu tariqa ustuvor navbatda eng katta element birinchi o‘ringa chiqadi. Ustuvor navbat shablonidan quyida sintaktik orqali foydalanish kerak:

priority_queue ;

Birinchi priority_queue so‘zini yozishimiz kerak.
Type ga navbatni to‘ldirish uchun kerakli tipni ko‘rsatishimiz lozim. Navbat obʻyektining nomini ko‘rsatishamiz kerak.
Ustivor navbatda yangi element qo‘shish uchun push() funksiyasi, birinchi o‘rindagi elementiga murojaat qilish uchun top() funksiyasi ishlatiladi. Front() – funksiyasi ishlatilmaydi. Shuningdek, back() – oxirgi elementga murojaat funksiyasi ham ishlatilmaydi.
4.7-dastur. Ustuvor navbatni (Priority_queue) yaratish va usullaridan foydalanish.


// Created by MBBahodir #include "stdafx.h" #include #include using namespace std;

int main() {






priority_queue priority_q, priority_q_one;
for (int h = 0; h < 7; h++) { priority_q.push(rand()%100);
}
priority_q_one = priority_q;
cout << "priority_q navbatdigi birinchi element " << priority_q.top() << endl; cout << " priority_q_one navbatdigi birinchi element " << priority_q_one.top() <<
endl;
priority_q.pop();
cout << "priority_q navbatdigi birinchi element (oʻchirishdan soʻng): " << priority_q.top() << endl;

cout << "priority_q navbat boʻsh " << (priority_q.empty()?"":"emas") << endl;


while (!priority_q_one.empty())
{
cout << "priority_q_one navbatdigi element : " << priority_q_one.top() <<
endl;
priority_q_one.pop();
}
cout << " priority_q_one navbat boʻsh " << (priority_q_one.empty()?"":"emas") << endl;
system("pause"); return 0;
}

Navbatlarni yaratishni ikki usulini kshrib chiqdik. Queue sinf shabloni va massiv asosida. Aslida ko‘proq queue sinf shablonidan foydalangan maqul. Chunki bu massivga nisbattan tez ishlaydi.

Download 0.66 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling