Map in STL

MAP


in C++ language we have an array to store similar data type and array have the indexing 0-based .but what if we want indexing rather than integer .to solve this kind of problem we have a map which is a container which helps us to store the values in pair. one is key and another is value and orders these pairs is according to keys. Map never contains duplicate key's.


EXAMPLE

let us suppose you want to store marks of the student then to do this you have to take two arrays's
let the name of array is marks[]  and name[] for keep name or mark on the same index in both arrays. it is difficult to deal with the array.we can also do this in easy way with the help of map which will store both name and mark as key and value by using the name of the student we directly get the mark of the student 




map is a generic class so you can use any type of primitive or user-defined data type in map. for using map in your program you have to include map header file.

INTERNAL IMPLEMENTATION OF MAP
It’s implemented as a self-balancing red-black tree.therefore it's searching time,insertion time,deletion time is log(n) Key is used to decide where to insert a new node inside RB tree. Value is just a data in the tree node.

SOME FREQUENTLY USED FUNCTION OF MAP


1.at(datatype k) --> return the value of a key , it's time complexity is O(logN).if key not found it throws the out_of_range exception.

2.begin() --> Return an iterator to the first element in the map it's time complexity is O(1)

3.end() --> Return an iterator to the last element in the map.it's time complecity is O(1)

4.find() --> very useful function used to find the key in map if key is not found it return's a theoritical element which follwos the last element of map it's time complexity is O(logN)

5.clear()-->it clear the whole map.erase all entries of map.it's time complecity is O(N).

6.erase()-->Removes a single element or the range of elements from the map

7.insert() -->it is used to insert new pair in map.it's time complecity is O(logN).

8.size()-->return the number of entries in the map.it's time complecity is O(1)

9.empty()-->return true if map is empty else false.it's time complecity is O(1)



Comments