Standard library header <map> - cppreference.com

This header is part of the

containers

library.

Includes

<compare>

(C++20)

Three-way comparison operator

support

[edit]

<initializer_list>

(C++11)

std::initializer_list

class template

[edit]

Classes

map

collection of key-value pairs, sorted by keys, keys are unique
(class template)

[edit]

multimap

collection of key-value pairs, sorted by keys
(class template)

[edit]

Functions

operator==operator!=operator<operator<=operator>operator>=operator<=>

(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)

lexicographically compares the values of two maps
(function template)

[edit]

std::swap(std::map)

specializes the

std::swap

algorithm
(function template)

[edit]

erase_if(std::map)

(C++20)

erases all elements satisfying specific criteria
(function template)

[edit]

operator==operator!=operator<operator<=operator>operator>=operator<=>

(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)

lexicographically compares the values of two multimaps
(function template)

[edit]

std::swap(std::multimap)

specializes the

std::swap

algorithm
(function template)

[edit]

erase_if(std::multimap)

(C++20)

erases all elements satisfying specific criteria
(function template)

[edit]

Range access

begincbegin

(C++11)(C++14)

returns an iterator to the beginning of a container or array
(function template)

[edit]

endcend

(C++11)(C++14)

returns an iterator to the end of a container or array
(function template)

[edit]

rbegincrbegin

(C++14)

returns a reverse iterator to the beginning of a container or array
(function template)

[edit]

rendcrend

(C++14)

returns a reverse end iterator for a container or array
(function template)

[edit]

sizessize

(C++17)(C++20)

returns the size of a container or array
(function template)

[edit]

empty

(C++17)

checks whether the container is empty
(function template)

[edit]

data

(C++17)

obtains the pointer to the underlying array
(function template)

[edit]

Synopsis

#include<compare>#include<initializer_list>namespacestd{// class template maptemplate<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>classmap;template<classKey,classT,classCompare,classAllocator>constexprbooloperator==(constmap<Key,T,Compare,Allocator>&x,constmap<Key,T,Compare,Allocator>&y);template<classKey,classT,classCompare,classAllocator>constexpr/*synth-three-way-result*/<pair<constKey,T>>operator<=>(constmap<Key,T,Compare,Allocator>&x,constmap<Key,T,Compare,Allocator>&y);template<classKey,classT,classCompare,classAllocator>constexprvoidswap(map<Key,T,Compare,Allocator>&x,map<Key,T,Compare,Allocator>&y)noexcept(noexcept(x.swap(y)));// erasure for maptemplate<classKey,classT,classCompare,classAllocator,classPredicate>constexprtypenamemap<Key,T,Compare,Allocator>::size_typeerase_if(map<Key,T,Compare,Allocator>&c,Predicatepred);// class template multimaptemplate<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>classmultimap;template<classKey,classT,classCompare,classAllocator>constexprbooloperator==(constmultimap<Key,T,Compare,Allocator>&x,constmultimap<Key,T,Compare,Allocator>&y);template<classKey,classT,classCompare,classAllocator>constexpr/*synth-three-way-result*/<pair<constKey,T>>operator<=>(constmultimap<Key,T,Compare,Allocator>&x,constmultimap<Key,T,Compare,Allocator>&y);template<classKey,classT,classCompare,classAllocator>constexprvoidswap(multimap<Key,T,Compare,Allocator>&x,multimap<Key,T,Compare,Allocator>&y)noexcept(noexcept(x.swap(y)));// erasure for multimaptemplate<classKey,classT,classCompare,classAllocator,classPredicate>constexprtypenamemultimap<Key,T,Compare,Allocator>::size_typeerase_if(multimap<Key,T,Compare,Allocator>&c,Predicatepred);namespacepmr{template<classKey,classT,classCompare=less<Key>>usingmap=std::map<Key,T,Compare,polymorphic_allocator<pair<constKey,T>>>;template<classKey,classT,classCompare=less<Key>>usingmultimap=std::multimap<Key,T,Compare,polymorphic_allocator<pair<constKey,T>>>;}}Class template

std::map

namespacestd{template<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>classmap{public:// typesusingkey_type=Key;usingmapped_type=T;usingvalue_type=pair<constKey,T>;usingkey_compare=Compare;usingallocator_type=Allocator;usingpointer=allocator_traits<Allocator>::pointer;usingconst_pointer=allocator_traits<Allocator>::const_pointer;usingreference=value_type&;usingconst_reference=constvalue_type&;usingsize_type=/* implementation-defined */;usingdifference_type=/* implementation-defined */;usingiterator=/* implementation-defined */;usingconst_iterator=/* implementation-defined */;usingreverse_iterator=std::reverse_iterator<iterator>;usingconst_reverse_iterator=std::reverse_iterator<const_iterator>;usingnode_type=/* unspecified */;usinginsert_return_type=/*insert-return-type*/<iterator,node_type>;classvalue_compare{protected:Comparecomp;constexprvalue_compare(Comparec):comp(c){}public:constexprbooloperator()(constvalue_type&x,constvalue_type&y)const{returncomp(x.first,y.first);}};// construct/copy/destroyconstexprmap():map(Compare()){}constexprexplicitmap(constCompare&comp,constAllocator&=Allocator());template<classInputIter>constexprmap(InputIterfirst,InputIterlast,constCompare&comp=Compare(),constAllocator&=Allocator());template<container-compatible-range<value_type>R>constexprmap(from_range_t,R&&rg,constCompare&comp=Compare(),constAllocator&=Allocator());constexprmap(constmap&x);constexprmap(map&&x);constexprexplicitmap(constAllocator&);constexprmap(constmap&,consttype_identity_t<Allocator>&);constexprmap(map&&,consttype_identity_t<Allocator>&);constexprmap(initializer_list<value_type>,constCompare&=Compare(),constAllocator&=Allocator());template<classInputIter>constexprmap(InputIterfirst,InputIterlast,constAllocator&a):map(first,last,Compare(),a){}template<container-compatible-range<value_type>R>constexprmap(from_range_t,R&&rg,constAllocator&a):map(from_range,std::forward<R>(rg),Compare(),a){}constexprmap(initializer_list<value_type>il,constAllocator&a):map(il,Compare(),a){}constexpr~map();constexprmap&operator=(constmap&x);constexprmap&operator=(map&&x)noexcept(allocator_traits<Allocator>::is_always_equal::value&&is_nothrow_move_assignable_v<Compare>);constexprmap&operator=(initializer_list<value_type>);constexprallocator_typeget_allocator()constnoexcept;// iteratorsconstexpriteratorbegin()noexcept;constexprconst_iteratorbegin()constnoexcept;constexpriteratorend()noexcept;constexprconst_iteratorend()constnoexcept;constexprreverse_iteratorrbegin()noexcept;constexprconst_reverse_iteratorrbegin()constnoexcept;constexprreverse_iteratorrend()noexcept;constexprconst_reverse_iteratorrend()constnoexcept;constexprconst_iteratorcbegin()constnoexcept;constexprconst_iteratorcend()constnoexcept;constexprconst_reverse_iteratorcrbegin()constnoexcept;constexprconst_reverse_iteratorcrend()constnoexcept;// capacityconstexprboolempty()constnoexcept;constexprsize_typesize()constnoexcept;constexprsize_typemax_size()constnoexcept;// element accessconstexprmapped_type&operator[](constkey_type&x);constexprmapped_type&operator[](key_type&&x);template<classK>constexprmapped_type&operator[](K&&x);constexprmapped_type&at(constkey_type&x);constexprconstmapped_type&at(constkey_type&x)const;template<classK>constexprmapped_type&at(constK&x);template<classK>constexprconstmapped_type&at(constK&x)const;// modifierstemplate<class...Args>constexprpair<iterator,bool>emplace(Args&&...args);template<class...Args>constexpriteratoremplace_hint(const_iteratorposition,Args&&...args);constexprpair<iterator,bool>insert(constvalue_type&x);constexprpair<iterator,bool>insert(value_type&&x);template<classP>constexprpair<iterator,bool>insert(P&&x);constexpriteratorinsert(const_iteratorposition,constvalue_type&x);constexpriteratorinsert(const_iteratorposition,value_type&&x);template<classP>constexpriteratorinsert(const_iteratorposition,P&&);template<classInputIter>constexprvoidinsert(InputIterfirst,InputIterlast);template<container-compatible-range<value_type>R>constexprvoidinsert_range(R&&rg);constexprvoidinsert(initializer_list<value_type>);constexprnode_typeextract(const_iteratorposition);constexprnode_typeextract(constkey_type&x);template<classK>constexprnode_typeextract(K&&x);constexprinsert_return_typeinsert(node_type&&nh);constexpriteratorinsert(const_iteratorhint,node_type&&nh);template<class...Args>constexprpair<iterator,bool>try_emplace(constkey_type&k,Args&&...args);template<class...Args>constexprpair<iterator,bool>try_emplace(key_type&&k,Args&&...args);template<classK,class...Args>constexprpair<iterator,bool>try_emplace(K&&k,Args&&...args);template<class...Args>constexpriteratortry_emplace(const_iteratorhint,constkey_type&k,Args&&...args);template<class...Args>constexpriteratortry_emplace(const_iteratorhint,key_type&&k,Args&&...args);template<classK,class...Args>constexpriteratortry_emplace(const_iteratorhint,K&&k,Args&&...args);template<classM>constexprpair<iterator,bool>insert_or_assign(constkey_type&k,M&&obj);template<classM>constexprpair<iterator,bool>insert_or_assign(key_type&&k,M&&obj);template<classK,classM>constexprpair<iterator,bool>insert_or_assign(K&&k,M&&obj);template<classM>constexpriteratorinsert_or_assign(const_iteratorhint,constkey_type&k,M&&obj);template<classM>constexpriteratorinsert_or_assign(const_iteratorhint,key_type&&k,M&&obj);template<classK,classM>constexpriteratorinsert_or_assign(const_iteratorhint,K&&k,M&&obj);constexpriteratorerase(iteratorposition);constexpriteratorerase(const_iteratorposition);constexprsize_typeerase(constkey_type&x);template<classK>constexprsize_typeerase(K&&x);constexpriteratorerase(const_iteratorfirst,const_iteratorlast);constexprvoidswap(map&)noexcept(allocator_traits<Allocator>::is_always_equal::value&&is_nothrow_swappable_v<Compare>);constexprvoidclear()noexcept;template<classC2>constexprvoidmerge(map<Key,T,C2,Allocator>&source);template<classC2>constexprvoidmerge(map<Key,T,C2,Allocator>&&source);template<classC2>constexprvoidmerge(multimap<Key,T,C2,Allocator>&source);template<classC2>constexprvoidmerge(multimap<Key,T,C2,Allocator>&&source);// observersconstexprkey_comparekey_comp()const;constexprvalue_comparevalue_comp()const;// map operationsconstexpriteratorfind(constkey_type&x);constexprconst_iteratorfind(constkey_type&x)const;template<classK>constexpriteratorfind(constK&x);template<classK>constexprconst_iteratorfind(constK&x)const;constexprsize_typecount(constkey_type&x)const;template<classK>constexprsize_typecount(constK&x)const;constexprboolcontains(constkey_type&x)const;template<classK>constexprboolcontains(constK&x)const;constexpriteratorlower_bound(constkey_type&x);constexprconst_iteratorlower_bound(constkey_type&x)const;template<classK>constexpriteratorlower_bound(constK&x);template<classK>constexprconst_iteratorlower_bound(constK&x)const;constexpriteratorupper_bound(constkey_type&x);constexprconst_iteratorupper_bound(constkey_type&x)const;template<classK>constexpriteratorupper_bound(constK&x);template<classK>constexprconst_iteratorupper_bound(constK&x)const;constexprpair<iterator,iterator>equal_range(constkey_type&x);constexprpair<const_iterator,const_iterator>equal_range(constkey_type&x)const;template<classK>constexprpair<iterator,iterator>equal_range(constK&x);template<classK>constexprpair<const_iterator,const_iterator>equal_range(constK&x)const;};template<classInputIter,classCompare=less</*iter-key-type*/<InputIter>>,classAllocator=allocator</*iter-to-alloc-type*/<InputIter>>>map(InputIter,InputIter,Compare=Compare(),Allocator=Allocator())->map</*iter-key-type*/<InputIter>,/*iter-mapped-type*/<InputIter>,Compare,Allocator>;template<ranges::input_rangeR,classCompare=less</*range-key-type*/<R>>,classAllocator=allocator</*range-to-alloc-type*/<R>>>map(from_range_t,R&&,Compare=Compare(),Allocator=Allocator())->map</*range-key-type*/<R>,/*range-mapped-type*/<R>,Compare,Allocator>;template<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>map(initializer_list<pair<Key,T>>,Compare=Compare(),Allocator=Allocator())->map<Key,T,Compare,Allocator>;template<classInputIter,classAllocator>map(InputIter,InputIter,Allocator)->map</*iter-key-type*/<InputIter>,/*iter-mapped-type*/<InputIter>,less</*iter-key-type*/<InputIter>>,Allocator>;template<ranges::input_rangeR,classAllocator>map(from_range_t,R&&,Allocator)->map</*range-key-type*/<R>,/*range-mapped-type*/<R>,less</*range-key-type*/<R>>,Allocator>;template<classKey,classT,classAllocator>map(initializer_list<pair<Key,T>>,Allocator)->map<Key,T,less<Key>,Allocator>;}Class template

std::multimap

namespacestd{template<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>classmultimap{public:// typesusingkey_type=Key;usingmapped_type=T;usingvalue_type=pair<constKey,T>;usingkey_compare=Compare;usingallocator_type=Allocator;usingpointer=allocator_traits<Allocator>::pointer;usingconst_pointer=allocator_traits<Allocator>::const_pointer;usingreference=value_type&;usingconst_reference=constvalue_type&;usingsize_type=/* implementation-defined */;usingdifference_type=/* implementation-defined */;usingiterator=/* implementation-defined */;usingconst_iterator=/* implementation-defined */;usingreverse_iterator=std::reverse_iterator<iterator>;usingconst_reverse_iterator=std::reverse_iterator<const_iterator>;usingnode_type=/* unspecified */;classvalue_compare{protected:Comparecomp;constexprvalue_compare(Comparec):comp(c){}public:constexprbooloperator()(constvalue_type&x,constvalue_type&y)const{returncomp(x.first,y.first);}};// construct/copy/destroyconstexprmultimap():multimap(Compare()){}constexprexplicitmultimap(constCompare&comp,constAllocator&=Allocator());template<classInputIter>constexprmultimap(InputIterfirst,InputIterlast,constCompare&comp=Compare(),constAllocator&=Allocator());template<container-compatible-range<value_type>R>constexprmultimap(from_range_t,R&&rg,constCompare&comp=Compare(),constAllocator&=Allocator());constexprmultimap(constmultimap&x);constexprmultimap(multimap&&x);constexprexplicitmultimap(constAllocator&);constexprmultimap(constmultimap&,consttype_identity_t<Allocator>&);constexprmultimap(multimap&&,consttype_identity_t<Allocator>&);constexprmultimap(initializer_list<value_type>,constCompare&=Compare(),constAllocator&=Allocator());template<classInputIter>constexprmultimap(InputIterfirst,InputIterlast,constAllocator&a):multimap(first,last,Compare(),a){}template<container-compatible-range<value_type>R>constexprmultimap(from_range_t,R&&rg,constAllocator&a):multimap(from_range,std::forward<R>(rg),Compare(),a){}constexprmultimap(initializer_list<value_type>il,constAllocator&a):multimap(il,Compare(),a){}constexpr~multimap();constexprmultimap&operator=(constmultimap&x);constexprmultimap&operator=(multimap&&x)noexcept(allocator_traits<Allocator>::is_always_equal::value&&is_nothrow_move_assignable_v<Compare>);constexprmultimap&operator=(initializer_list<value_type>);constexprallocator_typeget_allocator()constnoexcept;// iteratorsconstexpriteratorbegin()noexcept;constexprconst_iteratorbegin()constnoexcept;constexpriteratorend()noexcept;constexprconst_iteratorend()constnoexcept;constexprreverse_iteratorrbegin()noexcept;constexprconst_reverse_iteratorrbegin()constnoexcept;constexprreverse_iteratorrend()noexcept;constexprconst_reverse_iteratorrend()constnoexcept;constexprconst_iteratorcbegin()constnoexcept;constexprconst_iteratorcend()constnoexcept;constexprconst_reverse_iteratorcrbegin()constnoexcept;constexprconst_reverse_iteratorcrend()constnoexcept;// capacityconstexprboolempty()constnoexcept;constexprsize_typesize()constnoexcept;constexprsize_typemax_size()constnoexcept;// modifierstemplate<class...Args>constexpriteratoremplace(Args&&...args);template<class...Args>constexpriteratoremplace_hint(const_iteratorposition,Args&&...args);constexpriteratorinsert(constvalue_type&x);constexpriteratorinsert(value_type&&x);template<classP>constexpriteratorinsert(P&&x);constexpriteratorinsert(const_iteratorposition,constvalue_type&x);constexpriteratorinsert(const_iteratorposition,value_type&&x);template<classP>constexpriteratorinsert(const_iteratorposition,P&&x);template<classInputIter>constexprvoidinsert(InputIterfirst,InputIterlast);template<container-compatible-range<value_type>R>constexprvoidinsert_range(R&&rg);constexprvoidinsert(initializer_list<value_type>);constexprnode_typeextract(const_iteratorposition);constexprnode_typeextract(constkey_type&x);template<classK>node_typeextract(K&&x);constexpriteratorinsert(node_type&&nh);constexpriteratorinsert(const_iteratorhint,node_type&&nh);constexpriteratorerase(iteratorposition);constexpriteratorerase(const_iteratorposition);constexprsize_typeerase(constkey_type&x);template<classK>constexprsize_typeerase(K&&x);constexpriteratorerase(const_iteratorfirst,const_iteratorlast);constexprvoidswap(multimap&)noexcept(allocator_traits<Allocator>::is_always_equal::value&&is_nothrow_swappable_v<Compare>);constexprvoidclear()noexcept;template<classC2>constexprvoidmerge(multimap<Key,T,C2,Allocator>&source);template<classC2>constexprvoidmerge(multimap<Key,T,C2,Allocator>&&source);template<classC2>constexprvoidmerge(map<Key,T,C2,Allocator>&source);template<classC2>constexprvoidmerge(map<Key,T,C2,Allocator>&&source);// observersconstexprkey_comparekey_comp()const;constexprvalue_comparevalue_comp()const;// map operationsconstexpriteratorfind(constkey_type&x);constexprconst_iteratorfind(constkey_type&x)const;template<classK>constexpriteratorfind(constK&x);template<classK>constexprconst_iteratorfind(constK&x)const;constexprsize_typecount(constkey_type&x)const;template<classK>constexprsize_typecount(constK&x)const;constexprboolcontains(constkey_type&x)const;template<classK>constexprboolcontains(constK&x)const;constexpriteratorlower_bound(constkey_type&x);constexprconst_iteratorlower_bound(constkey_type&x)const;template<classK>constexpriteratorlower_bound(constK&x);template<classK>constexprconst_iteratorlower_bound(constK&x)const;constexpriteratorupper_bound(constkey_type&x);constexprconst_iteratorupper_bound(constkey_type&x)const;template<classK>constexpriteratorupper_bound(constK&x);template<classK>constexprconst_iteratorupper_bound(constK&x)const;constexprpair<iterator,iterator>equal_range(constkey_type&x);constexprpair<const_iterator,const_iterator>equal_range(constkey_type&x)const;template<classK>constexprpair<iterator,iterator>equal_range(constK&x);template<classK>constexprpair<const_iterator,const_iterator>equal_range(constK&x)const;};template<classInputIter,classCompare=less</*iter-key-type*/<InputIter>>,classAllocator=allocator</*iter-to-alloc-type*/<InputIter>>>multimap(InputIter,InputIter,Compare=Compare(),Allocator=Allocator())->multimap</*iter-key-type*/<InputIter>,/*iter-mapped-type*/<InputIter>,Compare,Allocator>;template<ranges::input_rangeR,classCompare=less</*range-key-type*/<R>>,classAllocator=allocator</*range-to-alloc-type*/<R>>>multimap(from_range_t,R&&,Compare=Compare(),Allocator=Allocator())->multimap</*range-key-type*/<R>,/*range-mapped-type*/<R>,Compare,Allocator>;template<classKey,classT,classCompare=less<Key>,classAllocator=allocator<pair<constKey,T>>>multimap(initializer_list<pair<Key,T>>,Compare=Compare(),Allocator=Allocator())->multimap<Key,T,Compare,Allocator>;template<classInputIter,classAllocator>multimap(InputIter,InputIter,Allocator)->multimap</*iter-key-type*/<InputIter>,/*iter-mapped-type*/<InputIter>,less</*iter-key-type*/<InputIter>>,Allocator>;template<ranges::input_rangeR,classAllocator>multimap(from_range_t,R&&,Allocator)->multimap</*range-key-type*/<R>,/*range-mapped-type*/<R>,less</*range-key-type*/<R>>,Allocator>;template<classKey,classT,classAllocator>multimap(initializer_list<pair<Key,T>>,Allocator)->multimap<Key,T,less<Key>,Allocator>;}Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 133

C++98

map::get_allocator

was missing in the synopsis added