set:
This container comes under Associative container, this is template class to store element of any type (in build as well as user defined data type). It stores the value and this value itself is key. There is NO duplicate value.
Header File:
#include<set>
Key Points:
- It sorts their element automatically based on sorting criteria.
- sorting criteria we can pass as argument while creating set object otherwise it will take default criteria that is less.
- No direct access or random access to element.
- Internally its implemented as Balanced Binary tree.
- There is NO push_back() and push_front() in it.
Source code of std::set with example:
/* Program: STL set Author: Alpha Master Date: 16 September 2021 */ //Header File #include<iostream> #include<set> //Display function the show the content of set void display(std::set<int>& st) { //using iterate to transverse the set and display the content std::set<int>::iterator itr; for(itr = st.begin(); itr != st.end(); itr++) { std::cout<<*itr<<" "; } std::cout<<std::endl; } int main() { std::cout<<"STL Set"<<std::endl; //Create empty set object std::set<int> st; std::cout<<"size:"<<st.size()<<std::endl; //Add element st.insert(5); st.insert(3); st.insert(4); st.insert(1); st.insert(2); std::cout<<"size:"<<st.size()<<std::endl; //display content display(st); return 0; }
Output:
STL Set size:0 size:5 1 2 3 4 5
Multi Set:
It allows duplicate element.