Sets


Sets are the associative containers.
There are mainly four types of associative containers: set, multi set, map and multi map.
Unlike the vectors, when elements are putted in sets and maps, the elements have association with each other. They contain a sorted set of unique objects of any type.
If we want to store user defined datatype (like any class's object) in set than we will have to provide compare function also, so that the data can be stored in set in sorted order.
we can pass the order of sorting the elements in set(ascending or descending), but by default the order is ascending.


set <datatype >  set_name; 
set_name.insert(any number);     //inserts elements in default ascending order.

set_name.erase(any number);    //Erases an integer val from the set .

set <datatype , greater<datatype> >  set_name;  
set_name.insert(any number);   // to store elements in descending order

set <datatype , less<datatype> >  set_name;     
set_name.insert(any number);    // to store elements in ascending order

set_name.clear();  // it is used to empty the set.

set_name.size();   // is used to find the size of set;










Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree