# Set vs Map : How to choose a right associative container ?

## Set VS Map : Choosing a right associative container

In this article we will discuss how the set and the map differ from one another and what we need to keep in mind when choosing a suitable container.

### Set :

• Set up an integral container that needs to store different items.
• It always keeps things in order.
• Inside it maintains a balanced binary search engine for items. So when searching for something within a set it only takes a log (n) to search for it.

Each item added within a set is constant i.e. you can not modify that item, because if you do, it will disrupt the internal data structure e.g. it will discard the internal assets of the binary search drug and lead to unexplained behavior.
Let’s take an example,

Create a set of different names for Departments and try to change one,

#include <iostream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>
void test()
{
std::set<std::string> setOfDepartments;
setOfDepartments.insert("One");
setOfDepartments.insert("Two");
setOfDepartments.insert("Three");
std::for_each(setOfDepartments.begin(), setOfDepartments.end(), [](std::string elem){
std::cout<<(elem)<<" , ";
});
// Trying to change the elements present
std::set<std::string>::iterator it = setOfDepartments.find("Two");
if(it != setOfDepartments.end())
{
std::cout << std::endl<< *it;
//*it = "Four"; // ALLOW is not done
}
}
int main()
{
test();
return 0;
}
Output :
One , Three , Two ,
Two

In the above example we cannot change an object using a iterator because here our complete object we have added to the SET is key and we can not change any key within the SET.

But what if we want to associate a certain value with this key and we want to change that corresponding value during operation. After that we need something other than SET and that is MAP.

### Map:

• A map is an integral container used to store key value items with unique keys.
• It always ends in pairs inserted in a sequential order based on the key.
• Inside it maintains a balanced binary search engine to store keys. So when the
• Search key inside the map only captures log (n) it is complex.
• We can not change the key for any paired feature on the map.
• We can change the value associated with the key to any included installation on the map.

Let’s take an example,

Create a door map with No of Employee count. Then search the Department and change its value relative to the number of employees.

#include <iostream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>
void testmap()
{
//  Department and Employee count in that Department by using MAP
std::map<std::string, int > mapOfDepEmpCount;
mapOfDepEmpCount.insert(std::make_pair("First", 0));
mapOfDepEmpCount.insert(std::make_pair("Second", 0));
mapOfDepEmpCount.insert(std::make_pair("Third", 0));
std::map<std::string, int >::iterator it = mapOfDepEmpCount.find("Second");
if(it != mapOfDepEmpCount.end())
{
std::cout << std::endl << "Department = "<< it->first << " :: No. Of Employees = " << it->second << std::endl;
// You can able to change the value associated with the key
it->second = 10;
//it->first = "sss"; // You are not able to change the key
}
it = mapOfDepEmpCount.find("Second");
if(it != mapOfDepEmpCount.end())
{
std::cout << std::endl << "Department = "<< it->first << " :: No. Of Employees = " << it->second << std::endl;
}
}
int main()
{
testmap();
return 0;
}

Output :
Department = Second :: No. Of Employees = 0
Department = Second :: No. Of Employees = 10

When to choose SET and when MAP?

Therefore, if we want to keep the data structure only different keys without the corresponding value that we plan to change in the future then use the set. If we want to change anything in set then delete it and install a new one.

Then we can use the map if we want to keep the data structure different keys and a specific value corresponding to each key that we want to modify in the future.