# Set in C++ STL (Standard Template Library)

C++ STL set container: Here, we are going to learn about the set container in C++ STL (Standard Template Library), how to use C++ STL to implement a set container?
Submitted by Radib Kar, on February 16, 2019

## Set in Data Structure

The set is an ordered container (entries are always sorted after insertion) where insertion and deletion can be done based on each entry. It's an associative container can be used to hold only the unique elements.

The main set operations are (basic ADT operations)...

1. insert (T data): Insert data to set
2. erase (cons T data): Deletes data from the set
3. bool empty(): Checks for set to be empty
4. find(const T data): finds whether data is present in the set or not

Where, T is the datatype (int / char / float etc..)

### STL

The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, sets etc. Therefore, Set can be implemented with help of STL too.

### STACK in STL

To declare a stack of datatype T:

```    set<T> st; //basic STL declarations
e.g.:
set<int> st; //set to hold integers only
```

To declare the set iterator

```    set<T> :: iterator it;
e.g.:
set<int>::iterator it;
```

### Functions available on STL set

1. insert (T data): Inserts data to set.
2. erase (const T data): Deletes data from the set.
3. empty(): Checks for set to be empty.
4. find(const T data) : Finds whether data is present in the set or not.
5. size(): Returns size of the set.
6. clear(): Clears the whole set.
7. lower_bound(T data): Returns an iterator to the first element that is equivalent to data or definitely does not go before the element data in the set.
8. upper_bound(T data): Returns an iterator to the first element that is equivalent to data or definitely will not go beyond the element data in the set.
9. emplace(T data): Inserts data only if data is unique based on elements already presented in set.

Kindly click on each function to check detailed code and implementation of each function, Below is the assemble of a total set operations mostly required.

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

void printSet(set<int> st){
set<int>:: iterator it;
cout<<"Set contents are:\n";
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}

int main(){
cout<<"Example of set STL\n";
set<int> st;
set<int>:: iterator it;

cout<<"inserting 4\n";
st.insert(4);
cout<<"inserting 6\n";
st.insert(6);
cout<<"inserting 10\n";
st.insert(10);

printSet(st); //printing current set

cout<<"erasing 6..\n";
st.erase(6); //prototype 1
cout<<"After erasing 6...\n";
printSet(st);

cout<<"erasing first element of the set now\n";
st.erase(st.begin());//prototype 2
cout<<"after erasing first element of set now\n";
printSet(st);

if(st.empty()) //checking for empty
cout<<"Set is empty\n";
else
cout<<"Set is not empty\n";

st.clear(); //clearing the set

cout<<"Clearing the set\n";
if(st.empty())
cout<<"Set is empty\n";
else
cout<<"Set is not empty\n";

return 0;
}
```

Output

```Example of set STL
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10
erasing 6..
After erasing 6...
Set contents are:
4 10
erasing first element of the set now
after erasing first element of set now
Set contents are:
10
Set is not empty
Clearing the set
Set is empty
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6621092,"math_prob":0.88441867,"size":3350,"snap":"2021-31-2021-39","text_gpt3_token_len":855,"char_repetition_ratio":0.14255828,"word_repetition_ratio":0.10881801,"special_character_ratio":0.27582088,"punctuation_ratio":0.17062937,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95580137,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-24T02:05:41Z\",\"WARC-Record-ID\":\"<urn:uuid:a782e330-c6a6-4d53-9301-0dbcc28a631c>\",\"Content-Length\":\"143310\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eb5c27c7-6a06-4b98-95a9-1ad52a7bd744>\",\"WARC-Concurrent-To\":\"<urn:uuid:b7f8fdb1-d668-41f4-b596-9a7ff4356b33>\",\"WARC-IP-Address\":\"172.67.140.245\",\"WARC-Target-URI\":\"https://www.includehelp.com/stl/set-in-cpp-stl.aspx\",\"WARC-Payload-Digest\":\"sha1:BCKMKJZWYN3Y4XM7IPHZ5WNHXD3BPXK3\",\"WARC-Block-Digest\":\"sha1:VQVF56LMJWT2KPF2DRZSAKOA5DXTJYAX\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057496.18_warc_CC-MAIN-20210924020020-20210924050020-00342.warc.gz\"}"}