📜  C++ Map

📅  最后修改于: 2020-10-20 05:30:12             🧑  作者: Mango

C++ STL Multimap.

Multimap是C++ STL(标准模板库)的一部分。多重地图是类似的容器,例如存储排序的键值对的地图,但是与仅存储唯一键的地图不同,多重地图可以具有重复的键。默认情况下,它使用<运算符比较键。

例如:一个雇员Multimap,其中雇员年龄是关键,姓名是值,可以表示为:

Keys Values
23 Nikita
28 Robin
25 Deep
25 Aman

Multimap员工具有重复的密钥年龄。

句法

template < class Key,                                     // multimap::key_type
           class T,                                                     // multimap::mapped_type
           class Compare = less,                        // multimap::key_compare
           class Alloc = allocator >    // multimap::allocator_type
           > class multimap;

参数

key:要存储在Multimap中的密钥数据类型。

type:要存储在Multimap中的值的数据类型。

compare:一个比较类,它接受两个bool类型相同的参数并返回一个值。此参数是可选的,二进制谓词less <“ key”>是默认值。

alloc:分配器对象的类型。此参数是可选的,默认值为allocator。

创建Multimap

使用以下语句可以轻松创建Multimap:

typedef pair value_type;

上面的表格将用于创建键类型为Key_type和值类型为value_type的Multimap。重要的一点是,Multimap的键和相应的值始终成对插入,不能在Multimap中仅插入键或仅插入值。

#include 
#include 
#include 

using namespace std;

int main()
{
    multimap m = {
            {"India","New Delhi"},
            {"India", "Hyderabad"},
            {"United Kingdom", "London"},
            {"United States", "Washington D.C"}
    };
    
    cout << "Size of map m: " << m.size() <::iterator it = m.begin(); it != m.end(); ++it)
    {
       cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
    }

    return 0;
}

输出:

Size of map m: 4
Elements in m: 
  [India, New Delhi]
  [India, Hyderabad]
  [United Kingdom, London]
  [United States, Washington D.C]

下面是multimap的所有成员函数的列表:

构造函数/析构函数

Functions Description
constructor Construct multimap
destructor Multimap destructor
operator= Copy elements of the multimap to another multimap.

迭代器

Functions Description
begin Returns an iterator pointing to the first element in the multimap.
cbegin Returns a const_iterator pointing to the first element in the multimap.
end Returns an iterator pointing to the past-end.
cend Returns a constant iterator pointing to the past-end.
rbegin Returns a reverse iterator pointing to the end.
rend Returns a reverse iterator pointing to the beginning.
crbegin Returns a constant reverse iterator pointing to the end.
crend Returns a constant reverse iterator pointing to the beginning.

容量

Functions Description
empty Return true if multimap is empty.
size Returns the number of elements in the multimap.
max_size Returns the maximum size of the multimap.

修饰符

Functions Description
insert Insert element in the multimap.
erase Erase elements from the multimap.
swap Exchange the content of the multimap.
clear Delete all the elements of the multimap.
emplace Construct and insert the new elements into the multimap.
emplace_hint Construct and insert new elements into the multimap by hint.

观察者

Functions Description
key_comp Return a copy of key comparison object.
value_comp Return a copy of value comparison object.

运作方式

Functions Description
find Search for an element with given key.
count Gets the number of elements matching with given key.
lower_bound Returns an iterator to lower bound.
upper_bound Returns an iterator to upper bound.
equal_range() Returns the range of elements matches with given key.

分配者

Functions Description
get_allocator Returns an allocator object that is used to construct the multimap.

非成员重载函数

Functions Description
operator== Checks whether the two multimaps are equal or not.
operator!= Checks whether the two multimaps are equal or not.
operator< Checks whether the first multimap is less than other or not.
operator<= Checks whether the first multimap is less than or equal to other or not.
operator> Checks whether the first multimap is greater than other or not.
operator>= Checks whether the first multimap is greater than equal to other or not.
swap() Exchanges the element of two multimaps.