Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Set in C++ Standard Template Library 
1.1.
Syntax of Set in C++
1.2.
Example for declaration of Set in C++
1.3.
Properties of Set in C++
2.
 
3.
 
4.
 
5.
Basic functions associated with Set in C++
6.
Code : 
6.1.
Output:
7.
Multiset
8.
Code
8.1.
Output
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

Set in C++ Standard Template Library

Author Apoorv
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Set in C++ Standard Template Library 

Sets are a sort of associative container in which each element must be distinct because it is identified by its value.

Syntax of Set in C++

set<datatype> nameofset;

Example for declaration of Set in C++

set<int> value = {6, 1, 52, 13}; 

Properties of Set in C++

  • A set's values are not indexed.
  • Each element in a set has a distinct value.
  • The element's value cannot be changed after it has been added to the set, but it is possible to remove the element and then re-add the modified value. As a result, the values are unchangeable.
  • Elements in the set are stored in the sorted order.

 

Ordered Set Unordered Set
It contains the unique elements and elements are stored in sorted order by default. Elements are not in sorted order
Implementation using Balanced BST Implemented using hashing
Present in #include<set> Present in #include<unordered_set>
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

 

 

 

Basic functions associated with Set in C++

insert()

emplace()

Used to insert an element in the set

s.insert(value)

s.emplace(value)

size() Tells the size of set return integer value s.size()
empty() Tells whether the set is empty or not  s.empty()
clear() Removes all the elements of set s.clear()
find() Will return the iterator where that element is present in the set auto it = s.find(value)
erase() Will delete that particular element in the set and will do the same in logarithmic time s.erase(value)
begin() It returns the iterator for the first element in the set auto it = s.begin()
end() It returns the iterator for the last element in the set auto it = s.end()
rbegin() It returns the reverse iterator.It points to the last element of set auto it = s.rbegin() 
rend() It points to the first element of the set and it is the reverse operator. auto it = s.rend() 
lower_bound(value) Returns an iterator to the first element in the set that is either equivalent to 'value' or will not go before the element 'value'. auto it = s.lower_bound(value) 
upper_bound(value) Returns an iterator to the first element in the set that will come after the element 'value'

 

auto it = s.upper_bound(value)

 

 

 

Code : 

// Implementation of Basic functions associated with Set in C++
#include <bits/stdc++.h>
using namespace std;


int main() {
    set<int>s;
    s.insert(1); // {1}
    s.emplace(2); // {1, 2}
    s.insert(2); // {1, 2}
    s.insert(4); // {1, 2, 4}
    s.insert(3); // {1, 2, 3, 4}


    // Printing size of set
    cout<<s.size()<<endl;


    // Printing elements of set
    for(auto i : s){
        cout<<i<<" ";
    }
    cout<<endl;

    // {1, 2, 3, 4, 5}
    auto it = s.find(3);
    cout<<*it<<endl;

    // {1, 2, 3, 4, 5}
    // auto it = s.find(6); 

    // {1, 4, 5}
    s.erase(5); // erases 5 // takes logarithmic time

    int cnt = s.count(1); 

    // auto it = s.find(3);
    // s.erase(it); // it takes constant time
    // {1, 2, 3, 4, 5}
    auto it1 = s.find(2);
    auto it2 = s.find(4);
    // after erase {1, 4, 5} [first, last)
    s.erase(it1, it2); 

    /*
        lower_bound() and upper_bound() function works in the same way
        as it works in case of vectors
        This is the syntax
        auto it = s.lower_bound(2); 
        auto it = s.upper_bound(3); 
   */
}

Output:

4

1 2 3 4 

3

Multiset

  • It contains duplicates
  • It is implemented using balanced BST
  • Elements are stored in the sorted order
  • It is present in #include<set>

Code

#include <bits/stdc++.h>
using namespace std;


int main() {

    // Everything is same as set in multiset only thing is that it stores the duplicate values
    multiset<int> m;
    m.insert(1); // {1}
    m.insert(1); // {1, 1}
    m.insert(1); // {1, 1, 1}

    // Size of multiset
    cout<<m.size()<<endl;

    // Print all the elements
    for(auto i:m)cout<<i<<endl;

    // all 1's erased from the multi set
    m.erase(1); 
    
    int count = m.count(1); 

    // only a single one erased
    m.erase(m.find(1));

    // rest all function will work same as set explained above
}

Output

3

1

1

1

FAQs

  1. How can we store the elements in descending order in sets?
    We can declare the set in this way set<int, greater<int> > s;
  2. What is the time complexity to find and delete an element in the set?
    log(n) where n is the total number of elements in the set.
  3. What is the real-life application of the set?
    We can use a set in those places where we have a requirement that every entity which we store in the set has only one occurrence. Suppose we have to store the details of a student and we know that two students with the same details are not possible. Hence we can use a set to store the students.

Key Takeaways

In this article, we have extensively discussed the theory of Set in C++ along with the implementation of some functions in C++.
Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding the set in C++, and if you would like to learn more, check out our articles on  Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!”

Next article
Map in C++
Live masterclass