Royxat erase(royxat begin)




Download 0,51 Mb.
Sana17.05.2024
Hajmi0,51 Mb.
#239960
Bog'liq
MUHAMMAD A2


MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI


Web dasturlash

Bajardi; Barchinoy Chiniqulova 061-22 guruh



1-ish

#include


#include
using namespace std;
// Ro'yxatdagi birinchi elementni olib tashlaydigan funksiya
template
T olibTashla(vector& royxat) {
if (royxat.empty()) {
cerr << "Xatolik: Ro'yxat bo'sh!" << endl;
exit(1); // Dasturdan chiqish
}
T element = royxat.front(); // Birinchi elementni olamiz
royxat.erase(royxat.begin()); // Ro'yxatdan birinchi elementni olib tashlaymiz
return element;
}
int main() {
// Int turlaridan iborat ro'yxat
vector intRoyxat = {1, 2, 3, 4, 5};
// Birinchi elementni olib tashlash
int birinchiInt = olibTashla(intRoyxat);
cout << "Birinchi element (int): " << birinchiInt << endl;
// Simvollardan iborat ro'yxat
vector charRoyxat = {'a', 'b', 'c', 'd', 'e'};
// Birinchi elementni olib tashlash
char birinchiChar = olibTashla(charRoyxat);
cout << "Birinchi element (char): " << birinchiChar << endl;
return 0;
}




2-ish
#include
#include
using namespace std;
// Ro'yxatdagi eng katta elementni o'chiruvchi funksiya
template
void engKattaOchir(vector& royxat) {
if (royxat.empty()) {
cerr << "Xatolik: Ro'yxat bo'sh!" << endl;
return;
}
typename vector::iterator engKatta = royxat.begin();
for (typename vector::iterator it = royxat.begin(); it != royxat.end(); ++it) {
if (*it > *engKatta) {
engKatta = it;
}
}
royxat.erase(engKatta); // Eng katta elementni o'chiramiz
}
int main() {
// Int turlaridan iborat ro'yxat
vector intRoyxat = {10, 5, 20, 15, 25};
// Ro'yxatni chop etish
cout << "Ro'yxat: ";
for (int el : intRoyxat) {
cout << el << " ";
}
cout << endl;
// Eng katta elementni o'chirish
engKattaOchir(intRoyxat);
// Yangi ro'yxatni chop etish
cout << "Eng katta elementni o'chirilgan so'ng: ";
for (int el : intRoyxat) {
cout << el << " ";
}
cout << endl;
return 0;
}


Download 0,51 Mb.




Download 0,51 Mb.