# Map and External Sorting Criteria/Comparator in C++ STL

Last Updated : 06 Apr, 2023

C++ Map is another commonly used STL container, it stores elements in a mapped fashion. Each element is stored as a pair having a key value and a mapped value. No two mapped values can have the same key values which means each key is unique. By default, key values in the map are in lexicographically sorted order. The map has two parts one is a key part and the other is the value part corresponding to the key. key and value can be of any data type (like int, string, float, etc).

Syntax :

`std:: map<key_datatype, value_datatype> map_name`

By default map stores the key in lexicographically sorted order but using external sorting criteria or the Comparator function, we can define our own custom sorting technique. To know more about maps refer to Map in C++.

## External Sorting Criteria/Comparator in C++ STL

Comparator functions are used to compare the objects of user-defined classes. The comparator function or Comparator class returns a boolean value, which basically tells us whether the passed “first” argument should be placed before the passed “second” argument or not and vice versa.

Syntax:

```struct cmp
{
/* data */
bool operator()(int a, int b) const
{
return a > b;
}
};```

Explanation:  The above comparator function operator() function take two pair of objects at a time. The condition will be designed as per the need of the problem in the comparator function. In the above example, the function returns true if the first integer is greater than the second integer.

Time Complexity:

Time Complexity : O(N)

Space Complexity : O(1)

As No Auxiliary Space is required for this external sorting algorithm

For example-  We know that by default map elements are sorted in ascending order according to the key value, But suppose we want the elements in the map to be stored in reverse lexicographical order of keys, Then in such a scenario we should use external sorting criteria i.e. function help to get scenario different than default condition.

Code:

## C++

 `// C++ Program to implement Map` `// with External Sorting` `// Criteria / Comparator` `#include ` `using` `namespace` `std;`   `// Comparator Function` `struct` `cmp {` `    ``bool` `operator()(``int` `a, ``int` `b) ``const` `{ ``return` `a > b; }` `};`   `int` `main()` `{` `    ``// Declaration of Map` `    ``map<``int``, ``int``> m1;`   `    ``// Insert element in map` `    ``// m[i]=x  Here, i is the key and m[i] is the value of` `    ``// the map` `    ``m1[0] = 5;` `    ``m1[1] = 6;` `    ``m1[2] = 7;` `    ``m1[3] = 8;` `    ``m1[4] = 9;`   `    ``// Without using External sorting criteria on map it` `    ``// looks like` `    ``for` `(``auto``& it : m1) {` `        ``cout << it.first << ``"->"` `<< it.second << endl;` `    ``}` `    ``cout << endl;`   `    ``// If we use External sorting Criteria on map` `    ``// then we have to take one more extra data type in map` `    ``// along with key and value of the map` `    ``map<``int``, ``int``, cmp> m2;`   `    ``// Here cmp is a user define data type in which we` `    ``// define our external sorting function` `    ``m2[0] = 5;` `    ``m2[1] = 6;` `    ``m2[2] = 7;` `    ``m2[3] = 8;` `    ``m2[4] = 9;`   `    ``//  using External sorting criteria on map it looks like` `    ``for` `(``auto``& it : m2) {` `        ``cout << it.first << ``"->"` `<< it.second << endl;` `    ``}`   `    ``return` `0;` `}`

Output

```0->5
1->6
2->7
3->8
4->9

4->9
3->8
2->7
1->6
0->5```

Explanation: In the above code, we have taken two different maps to show external sorting criteria. first map m1 and second map m2 stored the same type of key and value pair.

To sort a map using external sorting criteria we have to pass one more object as a new data type in the map declaration part which is basically structure or user define data type i, this extra data type which is used inside the map basically contains the logic of the comparator function.

Here, keys are sorted in descending order according to which map elements are sorted, As the Comparator function will return true only when the first integer is greater than the second integer.

Previous
Next