- 分享
map 所有函数
- @ 2024-8-17 14:09:38
构造函数
map(): 默认构造函数,创建一个空的map。map(const Compare& comp, const Allocator& alloc = Allocator()): 使用自定义比较函数和分配器构造map。map(InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator& alloc = Allocator()): 用一对迭代器的范围初始化map。map(const map& other): 拷贝构造函数。map(map&& other) noexcept: 移动构造函数。
赋值操作
map& operator=(const map& other): 拷贝赋值操作符。map& operator=(map&& other) noexcept: 移动赋值操作符。
迭代器
begin(): 返回指向map中第一个元素的迭代器。end(): 返回指向map中最后一个元素之后的迭代器。rbegin(): 返回指向map中最后一个元素的反向迭代器。rend(): 返回指向map中第一个元素之前的反向迭代器。
容量
empty() const: 检查map是否为空。size() const: 返回map中的元素数量。max_size() const: 返回map能够容纳的最大元素数量。
查找
find(const Key& key): 查找并返回一个迭代器,指向key对应的元素,如果不存在则返回end()。count(const Key& key) const: 返回key在map中的元素个数(map中每个键最多出现一次)。lower_bound(const Key& key): 返回指向key或大于key的第一个元素的迭代器。upper_bound(const Key& key): 返回指向key大于的第一个元素的迭代器。equal_range(const Key& key): 返回一个包含两个迭代器的pair,分别指向key的第一个匹配元素和最后一个匹配元素之后的位置。
插入和删除
insert(const value_type& value): 插入一个新的元素,如果key已经存在,则不插入。insert(value_type&& value): 移动插入一个新的元素。insert(InputIterator first, InputIterator last): 插入一对迭代器范围内的所有元素。emplace(Args&&... args): 使用给定的参数构造并插入一个新元素。emplace_hint(const_iterator hint, Args&&... args): 使用给定的参数和提示迭代器构造并插入一个新元素。erase(const_iterator position): 删除指定位置的元素。erase(const Key& key): 删除具有指定key的元素。erase(const_iterator first, const_iterator last): 删除指定范围内的所有元素。clear(): 删除所有元素。
访问
at(const Key& key): 返回一个对key对应值的引用,如果key不存在则抛出std::out_of_range异常。operator[](const Key& key): 如果key存在,返回对应的值;否则,插入一个新元素并返回其值的引用。
其他
swap(map& other) noexcept: 交换两个map的内容。get_allocator() const: 返回用于map的分配器。
0 条评论
目前还没有评论...