Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Set in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 103
    Comment on it

    Little introduction of Associative containers

    In the era of computing, Associative containers refer to a group of class templates in the standard library of the C++ programming language that implement ordered associative arrays.

    The following containers are defined in the current revision of the C++ standard: set, map, multiset, multimap. Each of these containers differ only on constraints placed on their elements. Associative containers are guaranteed to perform operations of insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n).

    Coming to Set
    Set is an associative container used to store unique elements in specific order. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.The value of an element is also the key used to identify it.

    Characteristics of Set

    Key uniqueness: in set each key must be unique.(Elements are inserted as keys)
    Element composition: In set and multiset each element is key; there are no mapped values.
    Element ordering: elements follow a strict weak ordering.(weak ordering is a mathematical formalization of the intuitive notion of a ranking of a set, some of whose members may be tied with each other.)

    A small demo program of set container

    #include <iostream>
    #include <set>
    
    using namespace std;
    
    int main()
    {
            int n;
            int i = 10;
            set <int> s; // declaring a set s of integer type
            set <int>:: iterator it;
            while (i--) {
                    cin >> n;
                    s.insert(n); // inserting integer value to it
            }
    
            cout << "Elements inside the set : ";    
            for ( it = s.begin(); it != s.end(); it++) {
                    cout << *it << " ";
            }
            cout << endl;
    
            return 0;
    }
    

    In the above program we have to insert 10 integer values into the set.
    Suppose my inputs are :

    4 4 4 4 5 5 5 4 3 3
    

    The output of the above program using the given input will be :

    Elements inside the set : 3 4 5
    

    Some Member functions of Set
    Iterators:
    begin
    end
    rbegin
    rend
    crbegin
    crend

    Capacity:
    empty
    size
    max_size

    Modifiers:
    insert
    erase
    swap
    clear
    emplace

    Operations:
    find
    count
    lower_bound
    upper_bound
    equal_range

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: