4-amaliy ish
Sana 05.05.2023 Hajmi 7,84 Kb. #935114
Bog'liq
Asadbek
4-amaliy ish
1-Topshiriq
a) Yangi konteynerlar hosil qiling, uni standartfunksiyalar yordamida elementlar bilan to’latingcount funksiyasi yordamida amallar bajaring
1)
#include
#include
#include
#include
#include
using namespace std;
int main()
{
//char va int toifasifa multimap
// Initsializator ro'yxati bilan boshlash
multimap mmapOfPos;
// mapga elementni kiritish
mmapOfPos.insert(std::pair('t', 9));
// Iterator yordamida multimap ustida takrorlanish
for (std::multimap::iterator it = mmapOfPos.begin();it != mmapOfPos.end(); it++)
cout << it->first << " :: " << it->second << endl;
cout << "***************************" << endl;
//count funksiyasidan foydalanish
int arr[] = { 14, 12, 15, 11, 10 };
//massivdan set ga initsializatsiya qilish
set s(arr, arr + 5);
// 11 ning bor yoki yo'qligini tekshirish
if (s.count(11))
cout << "set da 11 bor\n";
else
cout << "set da 11 yo'q\n";
// 18 bor yoki yo'qligini tekshirish
if (s.count(18))
cout << "set da 18 bor\n";
else
cout << "set da 18 yo'q\n";
return 0;}
b) Iteratorlardan foydalanib map konteyneri ustidaamallar bajaring;
2)
#include
#include
#include
using namespace std;
int main()
{
map gquiz1;
gquiz1.insert(pair(1, 40));
gquiz1.insert(pair(2, 30));
gquiz1.insert(pair(3, 60));
gquiz1.insert(pair(4, 20));
gquiz1.insert(pair(5, 50));
gquiz1.insert(pair(6, 50));
gquiz1.insert(pair(7, 10));
map::iterator itr;
cout << "\ngquiz1 mapi : \n";
cout << "\tKALIT\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
map gquiz2(gquiz1.begin(), gquiz1.end());
cout << "\ngquiz2 mapidan keyin" << "gquiz1 ning nusxasi: \n";
cout << "\tKALIT\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second << '\n';
}
cout << endl;
cout << "\ngquiz2 kalit = 3 dan kam elementlarni olibtashlangandan keyin : \n";
cout << "\tKALIT\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n";
cout << "\tKALIT\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second << '\n';
}
cout << endl;
cout << "gquiz1.lower_bound(5) : " << "\tKALIT = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second << endl;
cout << "gquiz1.upper_bound(5) : " << "\tKALIT = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second << endl;
return 0;
}
return 0;
}
c) fre () funksiyasidan foydalanib xotirani dinamiktaqsimlashni amalga oshiring .
3)
#include
#include
int main()
{
// Ushbu ko'rsatgich yaratilgan blokning asosiy manzilinisaqlab qoladi
int *ptr, *ptr1;
int n, i;
// Massiv elementlarini oling
n = 5;
printf("Elementlarni kiriting: %d\n", n);
// malloc () yordamida xotirani dinamik ravishda taqsimlang
ptr = (int*)malloc(n * sizeof(int));
// calloc() yordamida xotirani dinamik ravishda taqsimlang
ptr1 = (int*)calloc(n, sizeof(int));
// Xotira muvaffaqqiyatli ajratilgan yoki yo'qligini tekshirish
if (ptr == NULL || ptr1 == NULL) {
printf("Xotira muvaffaqqiyatli ajratilmadi\n");
exit(0);
} else {
// Xotira muvaffaqqiyatli ajratildi
printf("malloc() dan foydalangan holda xotiramuvaffaqqiyatli ajratildi\n");
// Xotirani bo'shating
free(ptr);
printf("malloc xotirasi muvaffaqiyatli bo'shatildi\n");
// Xotira muvaffaqqiyatli ajratildi
printf("\ncalloc() dan foydalangan holda xotiramuvaffaqqiyatli ajratildi\n");
// Xotirani bo'shating
printf("\calloc xotirasi muvaffaqqiyatli ajratildi");
}
return 0;
} Do'stlaringiz bilan baham: