C++ deque
C++ deque
Oldingi sahifada navbatdagi elementlar oxiriga qo‘shilishi va oldidan olib tashlanishini bilib oldingiz.
Deque (double-ended queue — ikki tomonlama navbat) esa moslashuvchanroq, chunki unda elementlarni ikkala uchidan (oldidan ham, orqasidan ham) qo‘shish va olib tashlash mumkin. Shuningdek, elementlarga indeks raqamlari orqali murojaat qilish ham mumkin.
Deque’dan foydalanish uchun <deque> sarlavha faylini qo‘shishingiz kerak:
// Include the deque library
#include <deque>
Deque yaratish
Deque yaratish uchun deque kalit so‘zidan foydalaning, burchakli qavslar <> ichida u saqlaydigan qiymatlar tipini, so‘ng deque nomini ko‘rsating, masalan: deque<type>
dequeName.
Misol
// Create a deque called cars that will store strings
deque<string> cars;
Agar e’lon qilish paytida elementlar qo‘shmoqchi bo‘lsangiz, ularni jingalak qavslar {} ichida vergul bilan ajratib yozing:
Misol
// Create a deque called cars that will store strings
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Print deque elements
for (string car : cars) {
cout << car << "\n";
}
O‘zingiz sinab ko‘ring »
Eslatma: Deque e’lon qilingandan keyin uning tipini (misolimizda string) o‘zgartirib bo‘lmaydi.
Deque elementlariga murojaat qilish
Deque elementiga kvadrat qavslar [] ichida indeks raqamini ko‘rsatib murojaat qilishingiz mumkin.
Deque’lar 0 dan indekslanadi, ya’ni [0] — birinchi element, [1] — ikkinchi element va hokazo:
Misol
// Create a deque called cars that will store strings
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Get the first element
cout << cars[0]; // Outputs Volvo
// Get the second element
cout << cars[1]; // Outputs BMW
O‘zingiz sinab ko‘ring »
Deque’ning birinchi yoki oxirgi elementiga .front() va .back() funksiyalari yordamida ham murojaat qilishingiz mumkin:
Misol
// Create a deque called cars that will store strings
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Get the first element
cout << cars.front();
// Get the last element
cout << cars.back();
O‘zingiz sinab ko‘ring »
Ko‘rsatilgan indeksdagi elementga murojaat qilish uchun .at() funksiyasidan foydalanib, indeks raqamini ko‘rsatishingiz mumkin:
Misol
// Create a deque called cars that will store strings
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Get the second element
cout << cars.at(1);
// Get the third element
cout << cars.at(2);
O‘zingiz sinab ko‘ring »
Eslatma: Ko‘pincha kvadrat qavslar [] o‘rniga .at() funksiyasi afzal ko‘riladi, chunki element diapazondan tashqarida bo‘lsa, u xato xabarini chiqaradi:
Misol
// Create a deque called cars that will store strings
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Try to access an element that does not exist (will throw an exception)
cout << cars.at(6);
O‘zingiz sinab ko‘ring »
Deque elementini o‘zgartirish
Muayyan element qiymatini o‘zgartirish uchun uning indeks raqamiga murojaat qilishingiz mumkin:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Change the value of the first element
cars[0] = "Opel";
cout << cars[0]; // Now outputs Opel instead of Volvo
O‘zingiz sinab ko‘ring »
Biroq .at() funksiyasidan foydalanish xavfsizroq:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Change the value of the first element
cars.at(0) = "Opel";
cout << cars.at(0); // Now outputs Opel instead of Volvo
O‘zingiz sinab ko‘ring »
Deque’ga element qo‘shish
Deque’ga element qo‘shish uchun uning boshiga element qo‘yishda .push_front() funksiyasidan, oxiriga element qo‘shishda esa .push_back() funksiyasidan foydalanishingiz mumkin:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Add an element at the beginning
cars.push_front("Tesla");
// Add an element at the end
cars.push_back("VW");
O‘zingiz sinab ko‘ring »
Deque’dan element olib tashlash
Deque’dan element olib tashlash uchun uning boshidagi elementni olib tashlashda .pop_front() funksiyasidan, oxiridagi elementni olib tashlashda esa .pop_back() funksiyasidan foydalaning:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
// Remove the first element
cars.pop_front();
// Remove the last element
cars.pop_back();
O‘zingiz sinab ko‘ring »
Deque o‘lchami
Deque’da nechta element borligini bilish uchun .size() funksiyasidan foydalaning:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars.size(); // Outputs 4
O‘zingiz sinab ko‘ring »
Deque bo‘shligini tekshirish
Deque bo‘sh yoki bo‘sh emasligini aniqlash uchun .empty() funksiyasidan foydalaning.
.empty() funksiyasi deque bo‘sh bo‘lsa
1 (true), aks holda 0 (false) qaytaradi:
Misol
deque<string> cars;
cout << cars.empty(); // Outputs 1 (The deque is empty)
O‘zingiz sinab ko‘ring »
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars.empty(); // Outputs 0 (not empty)
O‘zingiz sinab ko‘ring »
Deque bo‘ylab sikl
Deque elementlarini for siklini .size() funksiyasi bilan birga ishlatib aylanib chiqishingiz mumkin:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.size(); i++) {
cout << cars[i] << "\n";
}
O‘zingiz sinab ko‘ring »
Shuningdek, for-each siklidan ham foydalanishingiz mumkin (C++ 11-versiyasida (2011) kiritilgan) — u toza va o‘qilishi osonroq:
Misol
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (string car : cars) {
cout << car << "\n";
}
O‘zingiz sinab ko‘ring »
Maslahat: Deque’larni iterator yordamida ham aylanib chiqish mumkin, bu haqda keyingi boblarda batafsil bilib olasiz.
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
