Thursday, January 19, 2017

Sets in C++

Set in C++ :

STL in C++ are of great use when it comes to competitive programming. So here were are going to have look at the set stl. This post is based on what I learned about set and I have tried in simple english.


How to declare a map :

First include the header file #include

then you are ready to go with your map!!!

set<int> s;

To clear the set : 
     
      We can clear the function using clear() function.

s.clear();

To insert into the set :

      We can insert into a using insert() function.

s.insert(10);


To find whether an element exists in the set :

       We have to use find() function to check whether an element is present if it equals s.end()  element is not there in set.

cout<<s.find(10)==s.end();//prints 0 if 10 is not there


To erase an element in the set :

      Remember clear deletes the entire set whereas erase() function deletes just one element.

s.erase(10);

No comments:

Post a Comment