Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chèn và xóa trong STL Set C++


Chèn (Insertion)

Chèn trong STL set có thể dùng insert() và emplace().

Insert(): Insert() dùng để chèn phần tử vào set. Thao tác insert nhận tham chiếu đến một đối tượng.

Danh sách hàm dùng:

  • st.size() = Trả về kích thước set.
  • st.insert() = Dùng để chèn phần tử vào set.

Ví dụ mã

#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Display the set: "<<endl;
        cout<<"4.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<st.size()<<endl;
                break;
            case 2:
                cout<<"Enter value to be inserted: ";
                cin>>i;
                st.insert(i);
                break;
            case 3:
                cout<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 4:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

Kết quả

1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 1
Size of the Set: 0
...
(đã rút gọn)

Emplace()

Emplace cũng dùng để chèn phần tử vào set tại chỗ (in-place). Nó tránh sao chép đối tượng không cần thiết và chèn hiệu quả hơn insert.

Danh sách hàm dùng:

  • st.size() = Trả về kích thước set.
  • st.emplace() = Dùng để chèn phần tử vào set.

Ví dụ mã

#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Display the set: "<<endl;
        cout<<"4.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<st.size()<<endl;
                break;
            case 2:
                cout<<"Enter value to be inserted: ";
                cin>>i;
                st.emplace(i);
                break;
            case 3:
                cout<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 4:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

Kết quả

1.Size of the Set
2.Insert Element into the Set
3.Display the set:
4.Exit
Enter your Choice: 1
Size of the Set: 0
...
(đã rút gọn)

Xóa (Deletion)

Dùng hàm erase() để xóa phần tử từ set bằng cách chỉ định đối số: vị trí, giá trị hoặc phạm vi.

Danh sách hàm dùng:

  • st.size() = Trả về kích thước set.
  • st.insert() = Dùng để chèn phần tử vào set.
  • st.erase() = Xóa phần tử khỏi set.

Ví dụ mã

#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
    set<int> st;
    set<int>::iterator it;
    int c, i;
    while (1) {
        cout<<"1.Size of the Set"<<endl;
        cout<<"2.Insert Element into the Set"<<endl;
        cout<<"3.Delete Element from the Set"<<endl;
        cout<<"4.Display the set: "<<endl;
        cout<<"5.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Set: ";
                cout<<st.size()<<endl;
                break;
            case 2:
                cout<<"Enter value to be inserted: ";
                cin>>i;
                st.insert(i);
                break;
            case 3:
                cout<<"Enter the element to be deleted: ";
                cin>>i;
                st.erase(i);
                break;
            case 4:
                cout<<"Displaying Set by Iterator: ";
                for (it = st.begin(); it != st.end(); it++) {
                    cout << (*it)<<" ";
                }
                cout<<endl;
                break;
            case 5:
                exit(1);
                break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

Kết quả

1.Size of the Set
2.Insert Element into the Set
3.Delete Element from the Set
4.Display the set:
5.Exit
Enter your Choice: 1
Size of the Set: 0
...
(đã rút gọn)