67 / 90

C++ std::unordered_multiset

C++ da tartibsiz multisetlar elementlarni multiset kabi saqlaydi, lekin hech qanday maxsus tartibsiz, qiymat boʻyicha tez olish imkonini beradi.

Ko'p to'plamdagi kabi, har bir element o'z kaliti sifatida ishlaydi.

Tartibsiz multiset yarating

C++ da multisetni amalga oshirish uchun dasturimizga <unordered_set> sarlavha faylini kiritishimiz kerak.

example.cpp
#include <unordered_set>

Biz C++ da quyidagi sintaksisdan foydalanib tartibsiz multiset yaratishimiz mumkin:

example.cpp
// tartibsiz multisetni e'lon qiling
std::unordered_multiset<data_type> multiset_name = {key1, key2, key3, ...};

Bu yerga,

  • std::unordered_multiset - declares an STL container of type unordered_multiset. -<data_type> - multisetda saqlanadigan qiymatlarning ma'lumotlar turi. -multiset_name - multisetga berilgan noyob nom. -key1, key2, key3, ... - multisetda saqlanadigan kalit/qiymat.

Masalan,

example.cpp
// elementlar bilan tartibsiz multisetni ishga tushirish
std::multiset<int> unordered_multiset1 = {5, 3, 8, 1, 3};

// bo'sh tartibsiz multiset yaratish
std::multiset<int> unordered_multiset2;

[!NOTE] Oldinga qarab, biz std nom maydonidan foydalanamiz, shuning uchun std:: ni sintaksisimizdan chiqarib tashlashimiz mumkin.

1-misol: Tartibsiz ko'p to'plamni yarating

example.cpp
#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

    unordered_multiset<int> new_multiset = {5, 3, 8, 1, 3};
    
    for(int val : new_multiset) {
        cout << val << " ";
    }
    
    return 0;
}

Chiqish

kod
1 8 3 3 5

Bu erda biz int tipidagi tartibsiz multisetni yaratdik va elementlarning alohida tartibda saqlanmasligini ko'rishimiz mumkin.

C++ std::unordered_multiset usullari

C++ da multisetlar bilan foydalanishimiz mumkin bo'lgan ba'zi tez-tez ishlatiladigan usullar.

Funksiya tavsifi insert() Tartibsiz multisetga elementlarni kiriting. erase() Elementning barcha nusxalarini o'chirish. clear() Multisetdan barcha elementlarni olib tashlang. empty() Multiset bo'sh yoki yo'qligini tekshiring. size() Multisetning hajmini qaytaradi. found() Qiymatning paydo bo'lishini toping. count() Qiymatning chastotasini hisoblang.

2-misol: Tartibsiz multisetga elementlarni kiritish

insert() funksiyasidan foydalanib tartibsiz multisetga qiymatlar qo'shishimiz mumkin. Masalan,

example.cpp
#include <iostream>
#include <unordered_set> 
using namespace std;

int main () {

    unordered_multiset<int> my_unordered_multiset; 

    // unordered_multiset ga qiymat qo'shing
    my_unordered_multiset.insert(10);
    my_unordered_multiset.insert(30);
    my_unordered_multiset.insert(20);
    my_unordered_multiset.insert(50);
    my_unordered_multiset.insert(40);
    my_unordered_multiset.insert(50);

    // qo'shgandan so'ng unordered_multisetni chop eting
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }

    return 0;
}

Chiqish

kod
40  50  50  20  30  10

3-misol: Tartibsiz ko'p to'plamdan qiymatlarni o'chirish

Tartibsiz multisetdan oʻchirish uchun erase() va clear() funksiyalaridan foydalanishimiz mumkin:

  • erase() - deletes all instances... -clear() - multisetdan barcha elementlarni o'chiradi

Masalan,

example.cpp
#include <iostream>
#include <unordered_set> 
using namespace std;

int main () {

    unordered_multiset<int> my_unordered_multiset = {10, 30, 20, 50, 40, 50};

    // 50 ta barcha hodisalarni o'chirish
    my_unordered_multiset.erase(50);
    
    // o'chirishdan keyin unordered_multisetni chop eting
    cout << "\nThe unordered_multiset after erase: ";
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }
    
    // unordered_multiset dan barcha qiymatlarni o'chirib tashlang
    my_unordered_multiset.clear();
    
    // tozalashdan keyin unordered_multisetni chop eting
    cout << "\nThe unordered_multiset after clear: ";
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }
    
    return 0;
}

Chiqish

kod
The unordered_multiset after erase: 40  20  30  10  
The unordered_multiset after clear:

4-misol: C++ empty() va size() usullari

Biz empty() va size() sig'im usullaridan multisets bilan uning bo'sh yoki yo'qligini tekshirishimiz va mos ravishda hajmini olishimiz mumkin.

empty() usuli qaytadi

  • 1 (to'g'ri) - agar multiset bo'sh bo'lsa
  • 0 (noto'g'ri) - agar multiset bo'sh bo'lmasa

Masalan,

example.cpp
#include <iostream>
#include <unordered_set>
using namespace std;

int main () {

    unordered_multiset<int> my_unordered_multiset = {10, 20, 20, 20 ,30 , 40};

    // barcha qiymatlarni tozalashdan oldin unordered_multisetni chop eting
    cout << "The unordered_multiset before clear: ";
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }
    
    // unordered_multiset bo'sh yoki yo'qligini tekshiring
    cout << "\nEmpty: " << my_unordered_multiset.empty() << endl;

    // unordered_multiset hajmini tekshiring
    cout << "Size: " << my_unordered_multiset.size() << endl;
    
    // unordered_multiset dan barcha qiymatlarni o'chirib tashlang
    my_unordered_multiset.clear();
    
    // tozalashdan keyin unordered_multiset
    cout << "\nThe unordered_multiset after clear: ";
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }
    
    // sig'im usullaridan yana foydalaning
    cout << "\nEmpty: " << my_unordered_multiset.empty() << endl;
    cout << "Size: " << my_unordered_multiset.size() << endl;

    return 0;
}

Chiqish

kod
The unordered_multiset before clear: 40  30  20  20  20  10  
Empty: 0
Size: 6

The unordered_multiset after clear: 
Empty: 1
Size: 0

4-misol: C++ find() va count() usullari

Ko'p to'plamda berilgan qiymat mavjudligini tekshirish uchun find() va uning chastotasini tekshirish uchun count() dan foydalanamiz.

Masalan,

example.cpp
#include <iostream>
#include <unordered_set>
using namespace std;

int main () {

    unordered_multiset<int> my_unordered_multiset = {10, 20, 20, 20 ,30 , 40};

    // qidiruvdan oldin unordered_multisetni chop eting
    cout << "The unordered_multiset: ";
    for (int i : my_unordered_multiset) {
        cout << i << "  ";
    }

    // unordered_multiset ichida elementni toping
    int target = 20;
    unordered_multiset<int>::iterator it = my_unordered_multiset.find(target);

    if (it != my_unordered_multiset.end()) {
        cout << "\nFound " << target << " in the unordered_multiset." << endl;
    }
    else {
        cout << "\n" << target << " not found in the unordered_multiset." << endl;
    }

    // unordered_multisetdagi elementning takrorlanishini hisoblash
    int count_value = 20;
    int count_result = my_unordered_multiset.count(count_value);

    cout << count_value << " appears " << count_result << " times in the unordered_multiset." << endl;

    return 0;
}

Chiqish

kod
The unordered_multiset: 40  30  20  20  20  10  
Found 20 in the unordered_multiset.
20 appears 3 times in the unordered_multiset.

Yuqoridagi dasturda ushbu kodga e'tibor bering:

example.cpp
if (it != my_unordered_multiset.end()) {

Yuqoridagi shart qiymat find() funksiyasi tomonidan topilgan yoki topilmaganligini tekshiradi.

Agar qiymat topilsa, it iterator multisetdagi qiymatga mos keladigan elementga ishora qiladi.

Agar qiymat ko'p to'plamda topilmasa, it iterator my_unordered_multiset.end() ga teng bo'ladi, bu ko'p to'plamning oxiriga to'g'ri kelganligini ko'rsatadi.

[!NOTE] end() haqiqiy elementga ishora qilmaydi, lekin multisetdagi oxirgi elementdan keyingi joyni ifodalaydi.