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
Post a Comment