Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating va


Download 22.48 Kb.
Sana18.06.2023
Hajmi22.48 Kb.
#1591859
Bog'liq
3-misol javoblari


  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating va  for_each funksiyasi yordamida uni chop qiling

#include
#include
#include
int main() {
std::vector myVector = {1, 2, 3, 4, 5};
// Elementlar bilan to'ldirish
for (int i = 0; i < 5; i++) {
myVector.push_back(i);
}
// for_each funksiyasi yordamida chop qilish
std::for_each(myVector.begin(), myVector.end(), [](int element) {
std::cout << element << " ";
});
return 0;
}

  1. Iteratorlardan foydalanib list konteyneri ustida amallar bajaring

#include
#include

int main() {


std::list myList;

// Listga elementlarni qo'shamiz


myList.push_back(10);
myList.push_back(20);
myList.push_back(30);
myList.push_back(40);
myList.push_back(50);

// Listni elementlarini chiqaramiz


std::cout << "List: ";
for (auto it = myList.begin(); it != myList.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Listni teskari tartibda chiqaramiz


std::cout << "Teskari tartibda: ";
for (auto it = myList.rbegin(); it != myList.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Listni chapdan ikkinchi elementga o'zgartiramiz


auto it = myList.begin();
++it;
*it = 200;

// O'zgartirilgan listni chiqaramiz


std::cout << "O'zgartirilgan list: ";
for (auto it = myList.begin(); it != myList.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

return 0;


}

  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating find funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


// std::set yaratish
std::set mySet;

// Elementlarni qo'shish


mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
mySet.insert(40);
mySet.insert(50);

// Elementlar bilan to'la find funksiyasini ishlatish


auto it = mySet.find(30);
if (it != mySet.end()) {
std::cout << "30 topildi!" << std::endl;
} else {
std::cout << "30 topilmadi!" << std::endl;
}

// std::map yaratish


std::map myMap;

// Elementlarni qo'shish


myMap["apple"] = 10;
myMap["banana"] = 20;
myMap["orange"] = 30;
myMap["grape"] = 40;
myMap["watermelon"] = 50;

// Elementlar bilan to'la find funksiyasini ishlatish


auto iter = myMap.find("orange");
if (iter != myMap.end()) {
std::cout << "orange topildi! Qiymati: " << iter->second << std::endl;
} else {
std::cout << "orange topilmadi!" << std::endl;
}

return 0;


}

  1. Iteratorlardan foydalanib deque konteyneri ustida amallar bajaring

#include
#include

int main() {


std::deque myDeque;

// Elementlarni deque'ga qo'shamiz


myDeque.push_back(10);
myDeque.push_front(20);
myDeque.push_back(30);
myDeque.push_front(40);
myDeque.push_back(50);

// Deque ni iteratorlar orqali chiqaramiz


std::cout << "Deque: ";
for (auto it = myDeque.begin(); it != myDeque.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Deque elementlarini teskari tartibda chiqaramiz


std::cout << "Teskari tartibda: ";
for (auto it = myDeque.rbegin(); it != myDeque.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Deque ichidagi elementlarni o'zgartiramiz


auto it = myDeque.begin();
++it;
*it = 200;

// O'zgartirilgan deque ni chiqaramiz


std::cout << "O'zgartirilgan deque: ";
for (auto it = myDeque.begin(); it != myDeque.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

return 0;


};

  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating count funksiyasi yordamida amallar bajaring

#include
#include
#include
#include

int main() {


// std::vector yaratish
std::vector myVector = {10, 20, 30, 40, 50};

// Elementlarni chiqaramiz


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

// Elementlar sonini hisoblash


int count = std::count(myVector.begin(), myVector.end(), 30);
std::cout << "30 soni " << count << " marta paydo bo'ldi." << std::endl;

// std::set yaratish


std::set mySet = {"apple", "banana", "orange", "apple", "grape"};

// Elementlarni chiqaramiz


std::cout << "Set: ";
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;

// Elementlarni hisoblash


count = std::count(mySet.begin(), mySet.end(), "apple");
std::cout << "'apple' so'zi " << count << " marta paydo bo'ldi." << std::endl;

return 0;


}

  1. Iteratorlardan foydalanib map konteyneri ustida amallar bajaring

#include
#include

int main() {


std::map myMap;

// Elementlarni map'ga qo'shamiz


myMap["apple"] = 10;
myMap["banana"] = 20;
myMap["orange"] = 30;
myMap["grape"] = 40;
myMap["watermelon"] = 50;

// Mapni iteratorlar orqali chiqaramiz


std::cout << "Map: ";
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << ":" << it->second << " ";
}
std::cout << std::endl;

// Mapdan elementni topish


std::string key = "orange";
auto it = myMap.find(key);
if (it != myMap.end()) {
std::cout << "Element topildi: " << it->first << ":" << it->second << std::endl;
} else {
std::cout << "Element topilmadi!" << std::endl;
}

// Mapdan elementni o'chirish


key = "banana";
myMap.erase(key);

// O'chirilgan elementdan so'ng mapni chiqaramiz


std::cout << "O'chirilgan map: ";
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << ":" << it->second << " ";
}
std::cout << std::endl;

return 0;


}

  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating equal funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


// std::vector yaratish
std::vector vec1 = {1, 2, 3, 4, 5};
std::vector vec2 = {1, 2, 3, 4, 5};
std::vector vec3 = {5, 4, 3, 2, 1};

// Vec1 va vec2ni taqqoslash


bool result = std::equal(vec1.begin(), vec1.end(), vec2.begin(), vec2.end());
std::cout << "vec1 va vec2 bir xil: " << std::boolalpha << result << std::endl;

// Vec1 va vec3ni taqqoslash


result = std::equal(vec1.begin(), vec1.end(), vec3.begin(), vec3.end());
std::cout << "vec1 va vec3 bir xil: " << std::boolalpha << result << std::endl;

return 0;


}

  1. Iteratorlardan foydalanib vector konteyneri ustida amallar bajaring

#include
#include

int main() {


std::vector myVector = {10, 20, 30, 40, 50};

// Vectorning elementlarini chiqarish


std::cout << "Vector: ";
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Vectorning elementlarini o'zgartirish


auto it = myVector.begin();
++it;
*it = 200;

// O'zgartirilgan vectorni chiqarish


std::cout << "O'zgartirilgan vector: ";
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

// Vectorning elementlarini teskari tartibda chiqarish


std::cout << "Teskari tartibda: ";
for (auto it = myVector.rbegin(); it != myVector.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

return 0;


}

  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating search funksiyasi yordamida amallar bajaring;

#include
#include
#include

int main() {


// std::vector yaratish
std::vector myVector = {10, 20, 30, 40, 50, 60, 70, 80};

// Qidiruv uchun qator yaratish


std::vector subVector = {40, 50, 60};

// Vectorning elementlarini chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

// Qidiruv uchun qatori chiqarish


std::cout << "Qator: ";
for (const auto& element : subVector) {
std::cout << element << " ";
}
std::cout << std::endl;

// Qatorning vector ichida topilishini tekshirish


auto it = std::search(myVector.begin(), myVector.end(), subVector.begin(), subVector.end());
if (it != myVector.end()) {
std::cout << "Qator topildi indekslari: " << std::distance(myVector.begin(), it)
<< " - " << std::distance(myVector.begin(), it) + subVector.size() - 1 << std::endl;
} else {
std::cout << "Qator topilmadi!" << std::endl;
}

return 0;


}

  1. Iteratorlardan foydalanib multimap konteyneri ustida amallar bajaring

#include
#include

int main() {


std::multimap myMultimap;

// Elementlarni konteynerga qo'shish


myMultimap.insert(std::make_pair(1, "Apple"));
myMultimap.insert(std::make_pair(2, "Banana"));
myMultimap.insert(std::make_pair(3, "Orange"));
myMultimap.insert(std::make_pair(4, "Apple"));
myMultimap.insert(std::make_pair(5, "Grape"));
myMultimap.insert(std::make_pair(6, "Banana"));

// Konteynerdagi elementlarni chiqarish


std::cout << "Multimap: ";
for (const auto& pair : myMultimap) {
std::cout << "(" << pair.first << ", " << pair.second << ") ";
}
std::cout << std::endl;

// Elementni qidirish va chiqarish


int keyToFind = 2;
auto range = myMultimap.equal_range(keyToFind);
std::cout << "Elementlar " << keyToFind << " bilan to'liqish: ";
for (auto it = range.first; it != range.second; ++it) {
std::cout << "(" << it->first << ", " << it->second << ") ";
}
std::cout << std::endl;

return 0;


}

  1. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating copy funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector source = {1, 2, 3, 4, 5};
std::vector destination;

// source konteyneridagi elementlarni destination konteyneriga nusxalash


std::copy(source.begin(), source.end(), std::back_inserter(destination));

// destination konteyneridagi elementlarni chiqarish


std::cout << "Destination: ";
for (const auto& element : destination) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. #include

#include
#include
int main() {
std::vector source = {1, 2, 3, 4, 5};
std::vector destination;
// source konteyneridagi elementlarni destination konteyneriga nusxalash
std::copy(source.begin(), source.end(), std::back_inserter(destination));
// destination konteyneridagi elementlarni chiqarish
std::cout << "Destination: ";
for (const auto& element : destination) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}

  1. 8 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating swap funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector container1 = {1, 2, 3};
std::vector container2 = {4, 5, 6};

// Konteynerlarni almashtirish


std::swap(container1, container2);

// Almashtirilgan konteynerlarni chiqarish


std::cout << "Container1: ";
for (const auto& element : container1) {
std::cout << element << " ";
}
std::cout << std::endl;

std::cout << "Container2: ";


for (const auto& element : container2) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. Iteratorlardan foydalanib multiset konteyneri ustida amallar bajaring

#include
#include

int main() {


std::multiset myMultiset;

// Elementlarni konteynerga qo'shish


myMultiset.insert(10);
myMultiset.insert(30);
myMultiset.insert(20);
myMultiset.insert(30);
myMultiset.insert(40);

// Konteynerdagi elementlarni chiqarish


std::cout << "Multiset: ";
for (const auto& element : myMultiset) {
std::cout << element << " ";
}
std::cout << std::endl;

// Elementni qidirish va chiqarish


int valueToFind = 30;
auto range = myMultiset.equal_range(valueToFind);
std::cout << "Elementlar " << valueToFind << " bilan to'liqish: ";
for (auto it = range.first; it != range.second; ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;

return 0;


}

  1. 4 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating fill funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector(5);

// Konteynerdagi elementlarni qiymat bilan to'ldirish


std::fill(myVector.begin(), myVector.end(), 10);

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 10 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating remove funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {1, 2, 3, 4, 3, 5};

// Elementni konteynerdan olib tashlash


int valueToRemove = 3;
myVector.erase(std::remove(myVector.begin(), myVector.end(), valueToRemove), myVector.end());

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 12 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating unique funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {1, 2, 2, 3, 3, 3, 4, 5, 5};

// Konteynerdagi to'rivsiz elementlarni olib tashlash


myVector.erase(std::unique(myVector.begin(), myVector.end()), myVector.end());

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 15 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating reverse funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {1, 2, 3, 4, 5};

// Konteynerdagi elementlarni teskari tartibda joylashish


std::reverse(myVector.begin(), myVector.end());

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 2 bilan bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating rotate funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {1, 2, 3, 4, 5};

// Konteynerdagi elementlarni aylantirish


int rotateCount = 2;
std::rotate(myVector.begin(), myVector.begin() + rotateCount, myVector.end());

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 13 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating shuffle funksiyasi yordamida amallar bajaring

#include
#include
#include
#include

int main() {


std::vector myVector = {1, 2, 3, 4, 5};

// Konteynerdagi elementlarni aralashtirish


std::random_device rd;
std::mt19937 g(rd());
std::shuffle(myVector.begin(), myVector.end(), g);

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 16 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating sort funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {5, 2, 1, 4, 3};

// Konteynerdagi elementlarni tartiblash


std::sort(myVector.begin(), myVector.end());

// Konteynerdagi elementlarni chiqarish


std::cout << "Vector: ";
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 12 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating merge funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector vector1 = {1, 3, 5};
std::vector vector2 = {2, 4, 6};

std::vector mergedVector(vector1.size() + vector2.size());

// Konteynerlarni to'liqish
std::merge(vector1.begin(), vector1.end(), vector2.begin(), vector2.end(), mergedVector.begin());

// Birikmagan elementlarni chiqarish


std::cout << "Merged Vector: ";
for (const auto& element : mergedVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 10 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating set_union funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector vector1 = {1, 2, 3};
std::vector vector2 = {3, 4, 5};

std::vector unionVector(vector1.size() + vector2.size());

// Konteynerlarni to'liqish
std::set_union(vector1.begin(), vector1.end(), vector2.begin(), vector2.end(), unionVector.begin());

// Birikmagan elementlarni chiqarish


std::cout << "Union Vector: ";
for (const auto& element : unionVector) {
std::cout << element << " ";
}
std::cout << std::endl;

return 0;


}

  1. 8 bn bir xil

  2. Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating is_sorted_until funksiyasi yordamida amallar bajaring

#include
#include
#include

int main() {


std::vector myVector = {1, 3, 2, 4, 5};

// Elementlarni to'liqishga tekshirish


auto it = std::is_sorted_until(myVector.begin(), myVector.end());

// Elementlarni chiqarish


std::cout << "Sorted Prefix: ";
for (auto iter = myVector.begin(); iter != it; ++iter) {
std::cout << *iter << " ";
}
std::cout << std::endl;

return 0;


}

  1. 2 bn bir xil

Download 22.48 Kb.

Do'stlaringiz bilan baham:




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