目录
- 1、成员函数
- 2、元素访问
- 3、迭代器Iterators(C++ 11)
- 4、容量Capacity
- 5、修改函数(C++ 11和C++ 17)
- 6、查找表Lookup
- 7、观察Observers
- std::map是一个包含了key-value键值对映射的排序分配容器。利用比较函数对key进行排序。搜索,删除和插入操作具有对数复杂性。 Maps通常实现为红黑树。标准库的每个地方都使用比较要求,唯一性通过使用等价关系来确定。
1、成员函数
- map::map:构造函数,用来构造映射;
- map::~map:析构函数,在对象撤销回收时调用;
- map::operator=:分配值到容器中;
- map::get_allocator:返回相关的分配值。
2、元素访问
- map::at:使用边界检查访问指定的元素;
- map::operator[]:接入或插入指定的元素。
3、迭代器Iterators(C++ 11)
- map::begin/cbegin:返回一个迭代器给开始beginning.
- map::end/cend:返回一个迭代器给末尾end.
- map::rbegin/:crbegin:返回一个反向迭代器给开始beginning.
- map::rend/crend:返回一个反向迭代器给末尾end.
4、容量Capacity
- map::empty:检查容器是否为空。
- map::size:返回元素的数量。
- map::max_size:返回最大的可能元素数量。
5、修改函数(C++ 11和C++ 17)
- map::clear:清楚元素内容。
- map::insert:插入元素或节点。
- map::insert_or_assign:插入一个元素,当key存在时,就分配给当前的元素。
- map::emplace: 就地构造元素。
- map::emplace_hint:使用提示就地构造元素。
- map::try_emplace:如果key不存在,则就地插入元素;如果key存在,则不进行任何操作。
- map::erase:擦除元素。
- map::swap:交换内容。
- map::extract:从容器中抽取节点。
- map:merge:从另一个容器中拼接节点。
6、查找表Lookup
- map::count:返回匹配具体key的元素数量。
- map::find:查找具体key的元素。
- map::contains:检查容器是否包含具体key的元素。
- map::equal_range:返回匹配具体key的元素范围。
- map::lower_bound:将迭代器返回给第一个不小于给定键的元素。
- map::upper_bound:将迭代器返回给第一个大于给定键的元素
7、观察Observers
- map::key_comp:返回比较key的函数。
- map::value_comp:返回比较value_type类型的对象中的键的函数。
举例:
#include
#include
#include
#include
输出为:
map1 = {anything:199 something:69 that thing:50 }
iter = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
copied = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
moved = {anything:199 something:69 that thing:50 }
map1 = {}
init = {be:100 can:100 const:100 this:100 }
The magnitude of (-8, -15) is 17
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (-8, -15) is 17