Bài 18.5: Bài tập thực hành Set trong C++

1. Khai báo và Khởi tạo
#include <set>
#include <iostream>
#include <string>
#include <vector>

int main() {
    set<int> rong;
    cout << "Set rong duoc tao.\n";

    set<string> ten = {"Alice", "Bob", "Charlie", "Alice"};
    cout << "Set ten duoc tao voi cac ten: ";
    for (string s : ten) {
        cout << s << " ";
    }
    cout << "\n";

    vector<double> so = {3.14, 1.618, 2.718, 1.618, 0.577};
    set<double> duyNhat(so.begin(), so.end());
    cout << "Set so thuc duy nhat tu vector: ";
    for (double d : duyNhat) {
        cout << d << " ";
    }
    cout << "\n";

    return 0;
}

Output:

Set rong duoc tao.
Set ten duoc tao voi cac ten: Alice Bob Charlie 
Set so thuc duy nhat tu vector: 0.577 1.618 2.718 3.14
2. Thêm phần tử (insert)
#include <set>
#include <iostream>

int main() {
    set<int> s;

    auto kq1 = s.insert(10);
    auto kq2 = s.insert(5);
    auto kq3 = s.insert(20);
    auto kq4 = s.insert(10);

    cout << "KQ chen 10 (lan 1): " << (kq1.second ? "OK" : "KO") << "\n";
    cout << "KQ chen 5: " << (kq2.second ? "OK" : "KO") << "\n";
    cout << "KQ chen 20: " << (kq3.second ? "OK" : "KO") << "\n";
    cout << "KQ chen 10 (lan 2): " << (kq4.second ? "OK" : "KO") << "\n";

    cout << "Set sau chen: ";
    for (int v : s) {
        cout << v << " ";
    }
    cout << "\n";

    return 0;
}

Output:

KQ chen 10 (lan 1): OK
KQ chen 5: OK
KQ chen 20: OK
KQ chen 10 (lan 2): KO
Set sau chen: 5 10 20
3. Xóa phần tử (erase)
#include <set>
#include <iostream>

int main() {
    set<int> s = {10, 5, 20, 15, 25};

    cout << "Set dau: ";
    for (int v : s) {
        cout << v << " ";
    }
    cout << "\n";

    size_t xoa = s.erase(15);
    cout << "Da xoa " << xoa << " phan tu co gia tri 15.\n";

    xoa = s.erase(100);
    cout << "Da xoa " << xoa << " phan tu co gia tri 100.\n";

    cout << "Set sau xoa 15 va 100: ";
    for (int v : s) {
        cout << v << " ";
    }
    cout << "\n";

    auto it = s.find(10);
    if (it != s.end()) {
        s.erase(it);
        cout << "Da xoa phan tu tai iterator tro den (gia tri 10).\n";
    } else {
        cout << "Khong tim thay phan tu 10 de xoa.\n";
    }

    cout << "Set sau xoa 10 bang iterator: ";
    for (int v : s) {
        cout << v << " ";
    }
    cout << "\n";

    return 0;
}

Output:

Set dau: 5 10 15 20 25 
Da xoa 1 phan tu co gia tri 15.
Da xoa 0 phan tu co gia tri 100.
Set sau xoa 15 va 100: 5 10 20 25 
Da xoa phan tu tai iterator tro den (gia tri 10).
Set sau xoa 10 bang iterator: 5 20 25
4. Kiểm tra sự tồn tại (find, count)
#include <set>
#include <iostream>

int main() {
    set<int> s = {10, 5, 20};

    cout << "Set hien tai: ";
    for (int v : s) {
        cout << v << " ";
    }
    cout << "\n";

    auto timThay = s.find(10);
    if (timThay != s.end()) {
        cout << "Find: 10 CO trong set.\n";
    } else {
        cout << "Find: 10 KHONG co trong set.\n";
    }

    auto khongTimThay = s.find(100);
    if (khongTimThay != s.end()) {
        cout << "Find: 100 CO trong set.\n";
    } else {
        cout << "Find: 100 KHONG co trong set.\n";
    }

    cout << "---\n";

    if (s.count(10)) {
        cout << "Count: 10 CO trong set.\n";
    } else {
        cout << "Count: 10 KHONG co trong set.\n";
    }

    if (s.count(100)) {
        cout << "Count: 100 CO trong set.\n";
    } else {
        cout << "Count: 100 KHONG co trong set.\n";
    }

    return 0;
}

Output:

Set hien tai: 5 10 20 
Find: 10 CO trong set.
Find: 100 KHONG co trong set.
---
Count: 10 CO trong set.
Count: 100 KHONG co trong set.
5. Duyệt qua các phần tử
#include <set>
#include <iostream>
#include <string>

int main() {
    set<string> qua = {"banana", "apple", "orange", "grape"};

    cout << "Trai cay (sap xep tu dong):\n";

    for (auto it = qua.begin(); it != qua.end(); ++it) {
        cout << *it << "\n";
    }

    cout << "---\n";

    for (const string& s : qua) {
        cout << s << "\n";
    }

    return 0;
}

Output:

Trai cay (sap xep tu dong):
apple
banana
grape
orange
---
apple
banana
grape
orange
6. Kích thước và Trạng thái (size, empty)
#include <set>
#include <iostream>

int main() {
    set<int> s = {10, 5, 20};

    cout << "Kich thuoc set: " << s.size() << "\n";

    if (s.empty()) {
        cout << "Set dang rong.\n";
    } else {
        cout << "Set KHONG rong.\n";
    }

    set<int> rong;
    if (rong.empty()) {
        cout << "Set rong dang rong.\n";
    }

    return 0;
}

Output:

Kich thuoc set: 3
Set KHONG rong.
Set rong dang rong.
7. Xóa tất cả phần tử (clear)
#include <set>
#include <iostream>

int main() {
    set<int> s = {10, 5, 20, 15, 25};

    cout << "Kich thuoc set dau: " << s.size() << "\n";

    s.clear();

    cout << "Kich thuoc set sau clear(): " << s.size() << "\n";

    if (s.empty()) {
        cout << "Set da duoc lam rong.\n";
    }

    return 0;
}

Output:

Kich thuoc set dau: 5
Kich thuoc set sau clear(): 0
Set da duoc lam rong.

Một ví dụ thực tế hơn: Tìm các số duy nhất trong một danh sách

#include <set>
#include <vector>
#include <iostream>

int main() {
    vector<int> a = {1, 5, 2, 8, 2, 5, 10, 1, 3, 8};

    cout << "DS so ban dau: ";
    for (int n : a) {
        cout << n << " ";
    }
    cout << "\n";

    set<int> s;

    for (int n : a) {
        s.insert(n);
    }

    cout << "Cac so duy nhat (sap xep) trong DS:\n";
    for (int n : s) {
        cout << n << " ";
    }
    cout << "\n";

    set<int> s2(a.begin(), a.end());
    cout << "Khoi tao ngan gon tu vector: ";
    for (int n : s2) {
        cout << n << " ";
    }
    cout << "\n";

    return 0;
}

Output: ``` DS so ban dau: 1 5 2 8 2 5 10 1 3 8 Cac so duy nhat (sap xep) trong DS: 1 2 3 5 8 10 Khoi tao ngan gon tu vector: 1 2 3 5 8 10

Comments

There are no comments at the moment.