standard STL map header file


Under <map> header’s file there includes two template classes:map and multimap.Declaration of each of the classes is shown below.


namespace std {

template<typename Key , typename T , typename Compare= less<Key> ,
typename Allocator= allocator<pair< const Key , T>> >
class map ;

template<typename Key , typename T, typename Compare= less<Key> ,
typename Allocator = allocator<pair <const Key , T> > >
class multimap ;
}

Key : type of the key.
T : type of the value.
compare : less<Key> – a sorting criteria type
Allocator : allocator<pair const Key , T > – allocator that manages storage of pair type.

Link:Read about std:pair template here.

Requirements on key and value

The key and value type of the map and multimap class:Key and T,can be of any type.However,C++ impose certain requirements on them.

i)Both key and value must be copyable and movable.

ii)The key must be comparable with the sorting criteria/algorithm.


Alias of map type

The types of the map are given an alias for easier specification of the types.They are given below:

typedef key key_type ;key_type is a typedef of key type,meaning it signify the type of key.
typedef T mapped_type ; mapped_type signify the type T.
typedef pair<const Key , T> value_typevalue_type can be used as pair type that has Key and T as it’s first and second data member type.

STL map container

Some points to note about map container

*Manages element as key/value pair.

*Sort their elements using certain criteria/algorithm base on the key.

* Cannot have a duplicate key.

*Have a binary tree internal structure.

Link: map initialization ,accessing map.

Note we can change the sorting algorithm used by the map container.

Link: changing the sorting criteria/algorithm of STL containers -map , multimap , set ,etc

A *public member type

class value_compare;A member class of the map container which compares the key of the pair element.

   value_compare member function

operator()

All the member functions of the map template class is shown below with the parameters type and it’s return type.

constructor and destructor

map( ) noexcept;
explicit map(const Compare& comp , const Allocator& a = allocator_type() );
map(const map& x);
map(map&& x); (C++11)
map(initializer_list<value_type> l , const Compare& comp = Compare() ,const Allocator& a = allocator_type());(C++11)
explicit map(const Allocator& a);
map(const map& m , const Allocator& a);
map(map&& m, const Allocator& a);
map(initializer_list<value_type> l, const Allocator& a)
template< typename It>
map( It first , It last , const Allocator& a);
template<typename _InputIterator>
map(It first , It last) ;
template<typename _InputIterator>
map(It first, It last ,const Compare& comp , const Allocator& a = allocator_type());

Other member functions

Return
type
Function name and
argument
Function definition

Operator assignment
map&operator=(const map& x)
(There are two variant of this function)
assigns one object to another.

Allocator
Allocatorget_allocator() constReturns the allocator object manage by the map object

Iterator
iteratorbegin()Returns a read/write iterator of the first pair in the map
iteratorend()Returns a read/write iterator that points to one past the last pair in the map.
reverse_iteratorrbegin()Returns a read/write reverse iterator of the last pair in the map.
reverse_iteratorrend()Returns a read/write reverse iterator that points to one before the first pair in the map.
const_iteratorcbegin() const noexcept (C++11)Returns a read-only (constant) iterator that points to the first pair in the map.
const_iteratorcend() const noexceptReturns constant iterator that points one past the last pair in the map.
const_reverse_iteratorcrbegin() const noexceptReturns constant reverse iterator that points to the last pair in the map.
const_reverse_iterator crend() const noexceptReturns constant reverse iterator that points to one before the first pair in the map.

Capacity
bool empty() constReturns true if the map is empty.
size_t size() constReturns the size of the map.
size_tmax_size() constReturns the maximum size of the map.

Element access
mapped_type& operator[](const key_type& k)(There is a variant of this function)Access map element using the subscript [].
mapped_type&at(const key_type& k)Access map data.

Modifier
std::pair<iterator, bool>insert(const value_type& x)
(There are many variant of this function)
Insert pair into the map.
template <typename Ob>
pair<iterator, bool>
insert_or_assign(const key_type& k, Ob&& obj)
(There are many variant of this function)
Insert and assign pair into the map.
iteratorerase(const_iterator pos)
(There are many variant of this function)
Erases an element from a map.
void swap(map& x)Swaps data with another map.
void clear()Erases all elements in a map.
key_comparekey_comp() constReturns the key comparison object out of which the map was constructed.
value_comparevalue_comp() constReturns a value comparison object, built from the key comparison object out of which the map was constructed.

map operations
iteratorfind(const key_type& x)(There are many variant of this function)Locate element in a map
size_t count(const key_type& x) constFinds element with the given key.
iteratorlower_bound(const key_type& x)Finds the beginning of a subsequence matching given key.
iteratorupper_bound(const key_type& x)Finds the end of a subsequence matching given key.
pair<iterator, iterator>equal_range(const key_type& x)Finds a subsequence matching given key.

Template functions

template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator==(const map< Ky, Tp, Cpr , Al >& x,
const map< Ky, Tp, Cpr, Al>& y)
Equality comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator<(const map< Ky, Tp, Cpr , Al >& x,
const map< Ky, Tp, Cpr, Al>& y)
Lesser than comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator!=(const map< Ky, Tp, Cpr , Al >& x,
const map< Ky, Tp, Cpr, Al>& y)

(Non-member)
Not equal to comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator>(const map< Ky, Tp, Cpr , Al >& x,
const map< Ky, Tp, Cpr, Al>& y)

(Non-member)
Greater than comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator<=(const map< Ky, Tp, Cpr , Al >& x, const map< Ky, Tp, Cpr, Al>& y)
(Non-member)
Lesser or equality comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool operator=>(const map< Ky, Tp, Cpr , Al >& x, const map< Ky, Tp, Cpr, Al>& y)
(Non-member)
Greater than or equality comparison
template<typename Ky , typename T p, typename Cpr, typename Al> inline bool swap(map<Ky, Tp, Cpr, Al>& x , map< Ky, Tp, Cpr, Al>& y)
(Non-member)
Swap elements of two map object

 


Stop wasting time,earn money($$$) from your website-Join Now!