Set은 중복을 찾기에 최적화된 자료구조다.

#include <unordered_set>                // 0. include the library

int main() {
    // 1. initialize a hash set
    unordered_set<int> hashset;   
    // 2. insert a new key
    hashset.insert(3);
    hashset.insert(2);
    hashset.insert(1);
    // 3. delete a key
    hashset.erase(2);
    // 4. check if the key is in the hash set
    if (hashset.count(2) <= 0) {
        cout << "Key 2 is not in the hash set." << endl;
    }
    // 5. get the size of the hash set
    cout << "The size of hash set is: " << hashset.size() << endl; 
    // 6. iterate the hash set
    for (auto it = hashset.begin(); it != hashset.end(); ++it) {
        cout << (*it) << " ";
    }
    cout << "are in the hash set." << endl;
    // 7. clear the hash set
    hashset.clear();
    // 8. check if the hash set is empty
    if (hashset.empty()) {
        cout << "hash set is empty now!" << endl;
    }
}

'C++ STL' 카테고리의 다른 글

[C++][STL] fill, fill_n  (0) 2022.03.01
[C++][STL] set과 unordered_set의 차이  (0) 2021.11.11
[C++][STL] sort  (0) 2021.10.04
[C++][STL] tuple, tie  (0) 2021.09.26
[C++][STL] Queue  (0) 2021.09.26

+ Recent posts