Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน multiset::empty() ใน C++ STL

ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน multiset::empty() ใน C++ STL

Multiset ใน C++ STL คืออะไร?

Multisets เป็นคอนเทนเนอร์ที่คล้ายกับ set คือเก็บค่าในรูปแบบ key เหมือน set ในลำดับเฉพาะ

ใน multiset ค่าถูกระบุเป็น key เหมือน set ความแตกต่างหลักคือ set มี key แบบ distinct (ไม่ซ้ำ) ส่วน multiset สามารถมี key ซ้ำได้

Multiset keys ใช้ implement binary search trees

multiset::empty() คืออะไร?

ฟังก์ชัน multiset::empty() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <set>

ฟังก์ชันนี้ตรวจสอบว่าคอนเทนเนอร์ multiset ที่เกี่ยวข้องว่างหรือไม่

empty() ตรวจสอบขนาดคอนเทนเนอร์ หากเป็น 0 จะคืน true มิฉะนั้น false

ไวยากรณ์

ms_name.empty();

พารามิเตอร์

ฟังก์ชันไม่รับพารามิเตอร์

ค่าที่คืน

คืนค่า Boolean: true หากคอนเทนเนอร์ว่าง, false หากไม่ว่าง

ตัวอย่าง

Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4}; mymultiset.empty(); Output: false
Input: std::multiset<int> mymultiset; mymultiset.empty(); Output: true

ตัวอย่างโค้ด 1

#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[] = {2, 3, 4, 5};
    multiset<int> check(arr, arr + 4);
    if(check.empty())
        cout << "The multiset is empty";
    else
        cout << "The multiset isn't empty";
    return 0;
}

เอาต์พุต

The multiset isn't empty

ตัวอย่างโค้ด 2

#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[] = {};
    multiset<int> check(arr, arr + 0);
    if(check.empty())
        cout << "The multiset is empty";
    else
        cout << "The multiset isn't empty";
    return 0;
}

เอาต์พุต

The multiset is empty