Module 3 (Set and Map) - Algoritma-dan-Pemrograman-ITS/StrukturData GitHub Wiki

Set and Map

std::set and std::map are STL containers that implements Self-Balancing BST.

If std::set store a single value (key), std::map store a pair of value (key and value).

This module Will explains on how to use both STL containers.

std::set

  • set::empty()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int> s;
    
        if(s.empty()){
            cout << "This set is empty" << endl;
        }
    
        return 0;
    }
  • set::insert()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int> s;
    
        s.insert(4);
        s.insert(3);
        s.insert(2);
        s.insert(1);
    
        for(auto i = s.begin(); i != s.end(); i++){
            cout << *i << endl;
        }
    
        return 0;
    }

    std::set will automatically sort the data ascendingly. To sort decendingly, use function rbegin() and rend() or have a look on example below.

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int, greater<int>> s;
    
        s.insert(4);
        s.insert(3);
        s.insert(2);
        s.insert(1);
    
        for(auto i = s.begin(); i != s.end(); i++){
            cout << *i << endl;
        }
    
        return 0;
    }

    note: std::set couldn't store duplicate values

  • set::find()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int> s;
    
        s.insert(4);
        s.insert(3);
        s.insert(2);
        s.insert(1);
    
        auto pointer = s.find(5);
    
        if(pointer == s.end()){
            cout << "data not found" << endl;
        }
        else{
            cout << "data found" << endl;
        }
    
        return 0;
    }
  • set::erase()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int> s;
    
        s.insert(4);
        s.insert(3);
        s.insert(2);
        s.insert(1);
    
        s.erase(2);
        // doesn't use iterator
    
        for(auto i = s.begin(); i != s.end(); i++){
            cout << *i << endl;
        }
    
        return 0;
    }
    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        set<int> s;
    
        s.insert(4);
        s.insert(3);
        s.insert(2);
        s.insert(1);
    
        auto pointer = s.find(2);
    
        s.erase(pointer);
        // using iterator
    
        for(auto i = s.begin(); i != s.end(); i++){
            cout << *i << endl;
        }
    
        return 0;
    }

More on std::set could be read here or on C++ Language Documentation here.

std::map

  • map::empty()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        map<int, int> m;
    
        if(m.empty()){
            cout << "this map is empty" << endl;
        }
    
        return 0;
    }
  • map::insert()

    There are a lot of ways to insert values on std::map.

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        map<int, int> m;
        
        m.insert({1, 2});
        // directly
    
        m.insert(make_pair(2, 3));
        // use function make_pair
    
        m.insert(pair<int, int>(3, 4));
        // create pair object, similar to the example before
    
        m[4] = 5;
        // use indexing
    
        m[5]++;
        // increment
    
        for(auto i = m.begin(); i != m.end(); i++){
            cout << i->first << " " << i->second << endl;
        }
    
        return 0;
    }

    How to sort key on std::map descendingly?

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        map<int, int, greater<int>> m;
        
        m.insert({1, 2});
        m.insert(make_pair(2, 3));
        m.insert(pair<int, int>(3, 4));
        m[4] = 5;
        m[5]++;
    
        for(auto i = m.begin(); i != m.end(); i++){
            cout << i->first << " " << i->second << endl;
        }
    
        return 0;
    }
  • map::find()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        map<int, int> m;
    
        m.insert({1, 2});
        m.insert(make_pair(2, 3));
        m.insert(pair<int, int>(3, 4));
        m[4] = 5;
        m[5]++;
    
        auto pointer = m.find(1);
        /* 
        using function find
        return an iterator that points to an element with
        key = 1
        */
        if(pointer != m.end()){
            cout << pointer->first << " " << pointer->second << endl;
        }
    
        cout << m[5] << endl;
        /* 
        indexing, only shows the value of element with
        key = 5
        */
    
        return 0;
    }
  • map::erase()

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        map<int, int> m;
    
        m.insert({1, 2});
        m.insert(make_pair(2, 3));
        m.insert(pair<int, int>(3, 4));
        m[4] = 5;
        m[5]++;
        m[6] = 6;
    
        m.erase(1);
        // erase an element with key = 1
    
        auto pointer1 = m.find(2);
        m.erase(pointer1);
        // using iterator from function find
    
        auto pointer2 = m.find(5);
        m.erase(m.begin(), pointer2);
        // erase all element with key < 5
    
        m[5] = 0;
        /*
        not really erasing, only change value to 0
        on element with key = 5
        */
    
        for(auto i = m.begin(); i != m.end(); i++){
            cout << i->first << " " << i->second << endl;
        }
    
        return 0;
    }

More on std::map could be read here or on C++ Language Documentation here.

⚠️ **GitHub.com Fallback** ⚠️