|
|
@ -1,4 +1,4 @@
|
|
|
|
#ifndef MYTINYSTL_MAP_H_
|
|
|
|
#ifndef MYTINYSTL_MAP_H_ // 防止头文件被重复包含的预处理指令
|
|
|
|
#define MYTINYSTL_MAP_H_
|
|
|
|
#define MYTINYSTL_MAP_H_
|
|
|
|
|
|
|
|
|
|
|
|
// 这个头文件包含了两个模板类 map 和 multimap
|
|
|
|
// 这个头文件包含了两个模板类 map 和 multimap
|
|
|
@ -13,261 +13,295 @@
|
|
|
|
// * emplace_hint
|
|
|
|
// * emplace_hint
|
|
|
|
// * insert
|
|
|
|
// * insert
|
|
|
|
|
|
|
|
|
|
|
|
#include "rb_tree.h"
|
|
|
|
#include "rb_tree.h" // 包含红黑树的头文件,用于实现 map 和 multimap
|
|
|
|
|
|
|
|
|
|
|
|
namespace mystl
|
|
|
|
namespace mystl
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// 模板类 map,键值不允许重复
|
|
|
|
// 模板类 map,键值不允许重复
|
|
|
|
// 参数一代表键值类型,参数二代表实值类型,参数三代表键值的比较方式,缺省使用 mystl::less
|
|
|
|
template <class Key, class T, class Compare = mystl::less<Key>>
|
|
|
|
template <class Key, class T, class Compare = mystl::less<Key>>
|
|
|
|
class map
|
|
|
|
class map
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
|
|
|
|
// map 的嵌套型别定义
|
|
|
|
// map 的嵌套型别定义
|
|
|
|
typedef Key key_type;
|
|
|
|
typedef Key key_type; // 键类型
|
|
|
|
typedef T mapped_type;
|
|
|
|
typedef T mapped_type; // 映射值类型
|
|
|
|
typedef mystl::pair<const Key, T> value_type;
|
|
|
|
typedef mystl::pair<const Key, T> value_type; // 存储的值类型,键为 const,表示不会改变
|
|
|
|
typedef Compare key_compare;
|
|
|
|
typedef Compare key_compare; // 键的比较函数类型
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个 functor,用来进行元素比较
|
|
|
|
// 定义一个 functor,用来进行元素比较
|
|
|
|
class value_compare : public binary_function <value_type, value_type, bool>
|
|
|
|
class value_compare : public binary_function<value_type, value_type, bool>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
friend class map<Key, T, Compare>;
|
|
|
|
friend class map<Key, T, Compare>;
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
Compare comp;
|
|
|
|
Compare comp; // 比较函数对象
|
|
|
|
value_compare(Compare c) : comp(c) {}
|
|
|
|
value_compare(Compare c) : comp(c) {}
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
bool operator()(const value_type& lhs, const value_type& rhs) const
|
|
|
|
bool operator()(const value_type& lhs, const value_type& rhs) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return comp(lhs.first, rhs.first); // 比较键值的大小
|
|
|
|
return comp(lhs.first, rhs.first); // 比较两个元素的键
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
// 以 mystl::rb_tree 作为底层机制
|
|
|
|
// 以 mystl::rb_tree 作为底层机制
|
|
|
|
typedef mystl::rb_tree<value_type, key_compare> base_type;
|
|
|
|
typedef mystl::rb_tree<value_type, key_compare> base_type; // 红黑树类型
|
|
|
|
base_type tree_;
|
|
|
|
base_type tree_; // 红黑树实例
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 使用 rb_tree 的型别
|
|
|
|
// 使用 rb_tree 的型别
|
|
|
|
typedef typename base_type::node_type node_type;
|
|
|
|
typedef typename base_type::node_type node_type; // 节点类型
|
|
|
|
typedef typename base_type::pointer pointer;
|
|
|
|
typedef typename base_type::pointer pointer; // 指针类型
|
|
|
|
typedef typename base_type::const_pointer const_pointer;
|
|
|
|
typedef typename base_type::const_pointer const_pointer; // 常量指针类型
|
|
|
|
typedef typename base_type::reference reference;
|
|
|
|
typedef typename base_type::reference reference; // 引用类型
|
|
|
|
typedef typename base_type::const_reference const_reference;
|
|
|
|
typedef typename base_type::const_reference const_reference; // 常量引用类型
|
|
|
|
typedef typename base_type::iterator iterator;
|
|
|
|
typedef typename base_type::iterator iterator; // 迭代器类型
|
|
|
|
typedef typename base_type::const_iterator const_iterator;
|
|
|
|
typedef typename base_type::const_iterator const_iterator; // 常量迭代器类型
|
|
|
|
typedef typename base_type::reverse_iterator reverse_iterator;
|
|
|
|
typedef typename base_type::reverse_iterator reverse_iterator; // 反向迭代器类型
|
|
|
|
typedef typename base_type::const_reverse_iterator const_reverse_iterator;
|
|
|
|
typedef typename base_type::const_reverse_iterator const_reverse_iterator; // 常量反向迭代器类型
|
|
|
|
typedef typename base_type::size_type size_type;
|
|
|
|
typedef typename base_type::size_type size_type; // 大小类型
|
|
|
|
typedef typename base_type::difference_type difference_type;
|
|
|
|
typedef typename base_type::difference_type difference_type; // 差值类型
|
|
|
|
typedef typename base_type::allocator_type allocator_type;
|
|
|
|
typedef typename base_type::allocator_type allocator_type; // 分配器类型
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// 构造、复制、移动、赋值函数
|
|
|
|
// 构造、复制、移动、赋值函数
|
|
|
|
|
|
|
|
map() = default; // 默认构造函数
|
|
|
|
map() = default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
template <class InputIterator>
|
|
|
|
map(InputIterator first, InputIterator last)
|
|
|
|
map(InputIterator first, InputIterator last)
|
|
|
|
:tree_()
|
|
|
|
:tree_()
|
|
|
|
{ tree_.insert_unique(first, last); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.insert_unique(first, last);
|
|
|
|
|
|
|
|
} // 构造函数,使用迭代器范围插入元素
|
|
|
|
|
|
|
|
|
|
|
|
map(std::initializer_list<value_type> ilist)
|
|
|
|
map(std::initializer_list<value_type> ilist)
|
|
|
|
:tree_()
|
|
|
|
:tree_()
|
|
|
|
{ tree_.insert_unique(ilist.begin(), ilist.end()); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.insert_unique(ilist.begin(), ilist.end());
|
|
|
|
|
|
|
|
} // 构造函数,使用初始化列表插入元素
|
|
|
|
|
|
|
|
|
|
|
|
map(const map& rhs)
|
|
|
|
map(const map& rhs)
|
|
|
|
:tree_(rhs.tree_)
|
|
|
|
:tree_(rhs.tree_)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // 复制构造函数
|
|
|
|
map(map&& rhs) noexcept
|
|
|
|
map(map&& rhs) noexcept
|
|
|
|
:tree_(mystl::move(rhs.tree_))
|
|
|
|
:tree_(mystl::move(rhs.tree_))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // 移动构造函数
|
|
|
|
|
|
|
|
|
|
|
|
map& operator=(const map& rhs)
|
|
|
|
map& operator=(const map& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_ = rhs.tree_;
|
|
|
|
tree_ = rhs.tree_;
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 复制赋值运算符
|
|
|
|
map& operator=(map&& rhs)
|
|
|
|
map& operator=(map&& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_ = mystl::move(rhs.tree_);
|
|
|
|
tree_ = mystl::move(rhs.tree_);
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 移动赋值运算符
|
|
|
|
|
|
|
|
|
|
|
|
map& operator=(std::initializer_list<value_type> ilist)
|
|
|
|
map& operator=(std::initializer_list<value_type> ilist)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_.clear();
|
|
|
|
tree_.clear();
|
|
|
|
tree_.insert_unique(ilist.begin(), ilist.end());
|
|
|
|
tree_.insert_unique(ilist.begin(), ilist.end());
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 从初始化列表赋值
|
|
|
|
|
|
|
|
|
|
|
|
// 相关接口
|
|
|
|
// 相关接口
|
|
|
|
|
|
|
|
key_compare key_comp() const { return tree_.key_comp(); } // 获取键的比较函数
|
|
|
|
key_compare key_comp() const { return tree_.key_comp(); }
|
|
|
|
value_compare value_comp() const { return value_compare(tree_.key_comp()); } // 获取值的比较函数
|
|
|
|
value_compare value_comp() const { return value_compare(tree_.key_comp()); }
|
|
|
|
allocator_type get_allocator() const { return tree_.get_allocator(); } // 获取分配器
|
|
|
|
allocator_type get_allocator() const { return tree_.get_allocator(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 迭代器相关
|
|
|
|
// 迭代器相关
|
|
|
|
|
|
|
|
iterator begin() noexcept { return tree_.begin(); } // 返回开始的迭代器
|
|
|
|
|
|
|
|
const_iterator begin() const noexcept { return tree_.begin(); } // 返回开始的常量迭代器
|
|
|
|
|
|
|
|
iterator end() noexcept { return tree_.end(); } // 返回末尾的迭代器
|
|
|
|
|
|
|
|
const_iterator end() const noexcept { return tree_.end(); } // 返回末尾的常量迭代器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } // 返回反向开始的迭代器
|
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } // 返回反向开始的常量迭代器
|
|
|
|
|
|
|
|
reverse_iterator rend() noexcept { return reverse_iterator(begin()); } // 返回反向末尾的迭代器
|
|
|
|
|
|
|
|
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } // 返回反向末尾的常量迭代器
|
|
|
|
|
|
|
|
|
|
|
|
iterator begin() noexcept
|
|
|
|
const_iterator cbegin() const noexcept { return begin(); } // 返回开始的常量迭代器
|
|
|
|
{ return tree_.begin(); }
|
|
|
|
const_iterator cend() const noexcept { return end(); } // 返回末尾的常量迭代器
|
|
|
|
const_iterator begin() const noexcept
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return rbegin(); } // 返回反向开始的常量迭代器
|
|
|
|
{ return tree_.begin(); }
|
|
|
|
const_reverse_iterator crend() const noexcept { return rend(); } // 返回反向末尾的常量迭代器
|
|
|
|
iterator end() noexcept
|
|
|
|
|
|
|
|
{ return tree_.end(); }
|
|
|
|
|
|
|
|
const_iterator end() const noexcept
|
|
|
|
|
|
|
|
{ return tree_.end(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reverse_iterator rbegin() noexcept
|
|
|
|
|
|
|
|
{ return reverse_iterator(end()); }
|
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const noexcept
|
|
|
|
|
|
|
|
{ return const_reverse_iterator(end()); }
|
|
|
|
|
|
|
|
reverse_iterator rend() noexcept
|
|
|
|
|
|
|
|
{ return reverse_iterator(begin()); }
|
|
|
|
|
|
|
|
const_reverse_iterator rend() const noexcept
|
|
|
|
|
|
|
|
{ return const_reverse_iterator(begin()); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator cbegin() const noexcept
|
|
|
|
|
|
|
|
{ return begin(); }
|
|
|
|
|
|
|
|
const_iterator cend() const noexcept
|
|
|
|
|
|
|
|
{ return end(); }
|
|
|
|
|
|
|
|
const_reverse_iterator crbegin() const noexcept
|
|
|
|
|
|
|
|
{ return rbegin(); }
|
|
|
|
|
|
|
|
const_reverse_iterator crend() const noexcept
|
|
|
|
|
|
|
|
{ return rend(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 容量相关
|
|
|
|
// 容量相关
|
|
|
|
bool empty() const noexcept { return tree_.empty(); }
|
|
|
|
bool empty() const noexcept { return tree_.empty(); } // 检查是否为空
|
|
|
|
size_type size() const noexcept { return tree_.size(); }
|
|
|
|
size_type size() const noexcept { return tree_.size(); } // 获取大小
|
|
|
|
size_type max_size() const noexcept { return tree_.max_size(); }
|
|
|
|
size_type max_size() const noexcept { return tree_.max_size(); } // 获取最大容量
|
|
|
|
|
|
|
|
|
|
|
|
// 访问元素相关
|
|
|
|
// 访问元素相关
|
|
|
|
|
|
|
|
|
|
|
|
// 若键值不存在,at 会抛出一个异常
|
|
|
|
|
|
|
|
mapped_type& at(const key_type& key)
|
|
|
|
mapped_type& at(const key_type& key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
// it->first >= key
|
|
|
|
|
|
|
|
THROW_OUT_OF_RANGE_IF(it == end() || key_comp()(it->first, key),
|
|
|
|
THROW_OUT_OF_RANGE_IF(it == end() || key_comp()(it->first, key),
|
|
|
|
"map<Key, T> no such element exists");
|
|
|
|
"map<Key, T> no such element exists");
|
|
|
|
return it->second;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
} // 获取指定键的值,如果不存在则抛出异常
|
|
|
|
const mapped_type& at(const key_type& key) const
|
|
|
|
const mapped_type& at(const key_type& key) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const_iterator it = lower_bound(key);
|
|
|
|
const_iterator it = lower_bound(key);
|
|
|
|
// it->first >= key
|
|
|
|
|
|
|
|
THROW_OUT_OF_RANGE_IF(it == end() || key_comp()(it->first, key),
|
|
|
|
THROW_OUT_OF_RANGE_IF(it == end() || key_comp()(it->first, key),
|
|
|
|
"map<Key, T> no such element exists");
|
|
|
|
"map<Key, T> no such element exists");
|
|
|
|
return it->second;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
} // 获取指定键的值(常量版本),如果不存在则抛出异常
|
|
|
|
|
|
|
|
|
|
|
|
mapped_type& operator[](const key_type& key)
|
|
|
|
mapped_type& operator[](const key_type& key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
// it->first >= key
|
|
|
|
|
|
|
|
if (it == end() || key_comp()(key, it->first))
|
|
|
|
if (it == end() || key_comp()(key, it->first))
|
|
|
|
it = emplace_hint(it, key, T{});
|
|
|
|
it = emplace_hint(it, key, T{});
|
|
|
|
return it->second;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
} // 获取指定键的值,如果不存在则插入新元素
|
|
|
|
mapped_type& operator[](key_type&& key)
|
|
|
|
mapped_type& operator[](key_type&& key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
iterator it = lower_bound(key);
|
|
|
|
// it->first >= key
|
|
|
|
|
|
|
|
if (it == end() || key_comp()(key, it->first))
|
|
|
|
if (it == end() || key_comp()(key, it->first))
|
|
|
|
it = emplace_hint(it, mystl::move(key), T{});
|
|
|
|
it = emplace_hint(it, mystl::move(key), T{});
|
|
|
|
return it->second;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
} // 获取指定键的值(移动版本),如果不存在则插入新元素
|
|
|
|
|
|
|
|
|
|
|
|
// 插入删除相关
|
|
|
|
// 插入删除相关
|
|
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
template <class ...Args>
|
|
|
|
pair<iterator, bool> emplace(Args&& ...args)
|
|
|
|
pair<iterator, bool> emplace(Args&& ...args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.emplace_unique(mystl::forward<Args>(args)...);
|
|
|
|
return tree_.emplace_unique(mystl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
} // 原地构造元素并插入
|
|
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
template <class ...Args>
|
|
|
|
iterator emplace_hint(iterator hint, Args&& ...args)
|
|
|
|
iterator emplace_hint(iterator hint, Args&& ...args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...);
|
|
|
|
return tree_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
} // 原地构造元素并插入,使用 hint 提示位置
|
|
|
|
|
|
|
|
|
|
|
|
pair<iterator, bool> insert(const value_type& value)
|
|
|
|
pair<iterator, bool> insert(const value_type& value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_unique(value);
|
|
|
|
return tree_.insert_unique(value);
|
|
|
|
}
|
|
|
|
} // 插入元素
|
|
|
|
pair<iterator, bool> insert(value_type&& value)
|
|
|
|
pair<iterator, bool> insert(value_type&& value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_unique(mystl::move(value));
|
|
|
|
return tree_.insert_unique(mystl::move(value));
|
|
|
|
}
|
|
|
|
} // 插入移动构造的元素
|
|
|
|
|
|
|
|
|
|
|
|
iterator insert(iterator hint, const value_type& value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.insert_unique(hint, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator insert(iterator hint, value_type&& value)
|
|
|
|
iterator insert(iterator hint, value_type&& value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_unique(hint, mystl::move(value));
|
|
|
|
return tree_.insert_unique(hint, mystl::move(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 插入元素,并使用 hint 提示位置,同时使用移动语义
|
|
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
template <class InputIterator>
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_.insert_unique(first, last);
|
|
|
|
tree_.insert_unique(first, last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 插入迭代器区间内的所有元素
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void erase(iterator position)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.erase(position);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 擦除指定位置的元素
|
|
|
|
|
|
|
|
|
|
|
|
void erase(iterator position) { tree_.erase(position); }
|
|
|
|
size_type erase(const key_type& key)
|
|
|
|
size_type erase(const key_type& key) { return tree_.erase_unique(key); }
|
|
|
|
{
|
|
|
|
void erase(iterator first, iterator last) { tree_.erase(first, last); }
|
|
|
|
return tree_.erase_unique(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 擦除指定键的所有元素,并返回擦除的元素数量
|
|
|
|
|
|
|
|
|
|
|
|
void clear() { tree_.clear(); }
|
|
|
|
void erase(iterator first, iterator last)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.erase(first, last);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 擦除[first, last)区间内的元素
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 清空 map
|
|
|
|
|
|
|
|
|
|
|
|
// map 相关操作
|
|
|
|
// map 相关操作
|
|
|
|
|
|
|
|
iterator find(const key_type& key)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.find(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查找键对应的迭代器,如果不存在则返回 end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator find(const key_type& key) const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.find(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查找键对应的迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
iterator find(const key_type& key) { return tree_.find(key); }
|
|
|
|
size_type count(const key_type& key) const
|
|
|
|
const_iterator find(const key_type& key) const { return tree_.find(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.count_unique(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指定键的元素数量(由于 map 的键不重复,结果为 1 或 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator lower_bound(const key_type& key)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.lower_bound(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指向键值大于或等于给定键的首个迭代器
|
|
|
|
|
|
|
|
|
|
|
|
size_type count(const key_type& key) const { return tree_.count_unique(key); }
|
|
|
|
const_iterator lower_bound(const key_type& key) const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.lower_bound(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指向键值大于或等于给定键的首个迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
iterator lower_bound(const key_type& key) { return tree_.lower_bound(key); }
|
|
|
|
iterator upper_bound(const key_type& key)
|
|
|
|
const_iterator lower_bound(const key_type& key) const { return tree_.lower_bound(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.upper_bound(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指向键值大于给定键的首个迭代器
|
|
|
|
|
|
|
|
|
|
|
|
iterator upper_bound(const key_type& key) { return tree_.upper_bound(key); }
|
|
|
|
const_iterator upper_bound(const key_type& key) const
|
|
|
|
const_iterator upper_bound(const key_type& key) const { return tree_.upper_bound(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.upper_bound(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指向键值大于给定键的首个迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
pair<iterator, iterator>
|
|
|
|
pair<iterator, iterator>
|
|
|
|
equal_range(const key_type& key)
|
|
|
|
equal_range(const key_type& key)
|
|
|
|
{ return tree_.equal_range_unique(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.equal_range_unique(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指定键值的范围
|
|
|
|
|
|
|
|
|
|
|
|
pair<const_iterator, const_iterator>
|
|
|
|
pair<const_iterator, const_iterator>
|
|
|
|
equal_range(const key_type& key) const
|
|
|
|
equal_range(const key_type& key) const
|
|
|
|
{ return tree_.equal_range_unique(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.equal_range_unique(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回指定键值的范围(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
void swap(map& rhs) noexcept
|
|
|
|
void swap(map& rhs) noexcept
|
|
|
|
{ tree_.swap(rhs.tree_); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.swap(rhs.tree_);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 交换两个 map 对象的内容
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
friend bool operator==(const map& lhs, const map& rhs) { return lhs.tree_ == rhs.tree_; }
|
|
|
|
friend bool operator==(const map& lhs, const map& rhs)
|
|
|
|
friend bool operator< (const map& lhs, const map& rhs) { return lhs.tree_ < rhs.tree_; }
|
|
|
|
{
|
|
|
|
};
|
|
|
|
return lhs.tree_ == rhs.tree_;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 比较两个 map 是否相等
|
|
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
friend bool operator<(const map& lhs, const map& rhs)
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
{
|
|
|
|
bool operator==(const map<Key, T, Compare>& lhs, const map<Key, T, Compare>& rhs)
|
|
|
|
return lhs.tree_ < rhs.tree_;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return lhs == rhs;
|
|
|
|
// 比较两个 map 的字典序
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
// 其他比较运算符的重载...
|
|
|
|
bool operator<(const map<Key, T, Compare>& lhs, const map<Key, T, Compare>& rhs)
|
|
|
|
};
|
|
|
|
{
|
|
|
|
|
|
|
|
return lhs < rhs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符,允许直接比较两个 map 对象...
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
bool operator!=(const map<Key, T, Compare>& lhs, const map<Key, T, Compare>& rhs)
|
|
|
|
bool operator!=(const map<Key, T, Compare>& lhs, const map<Key, T, Compare>& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -292,45 +326,42 @@ bool operator>=(const map<Key, T, Compare>& lhs, const map<Key, T, Compare>& rhs
|
|
|
|
return !(lhs < rhs);
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
// 重载 mystl 的 swap 函数,使得 std::swap 可以直接使用
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
void swap(map<Key, T, Compare>& lhs, map<Key, T, Compare>& rhs) noexcept
|
|
|
|
void swap(map<Key, T, Compare>& lhs, map<Key, T, Compare>& rhs) noexcept
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模板类 multimap,键值允许重复
|
|
|
|
// 模板类 multimap,键值允许重复
|
|
|
|
// 参数一代表键值类型,参数二代表实值类型,参数三代表键值的比较方式,缺省使用 mystl::less
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare = mystl::less<Key>>
|
|
|
|
template <class Key, class T, class Compare = mystl::less<Key>>
|
|
|
|
class multimap
|
|
|
|
class multimap
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// multimap 的型别定义
|
|
|
|
// multimap 的型别定义
|
|
|
|
typedef Key key_type;
|
|
|
|
typedef Key key_type; // 键类型
|
|
|
|
typedef T mapped_type;
|
|
|
|
typedef T mapped_type; // 映射值类型
|
|
|
|
typedef mystl::pair<const Key, T> value_type;
|
|
|
|
typedef mystl::pair<const Key, T> value_type; // 存储的值类型,键为 const,表示不会改变
|
|
|
|
typedef Compare key_compare;
|
|
|
|
typedef Compare key_compare; // 键的比较函数类型
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个 functor,用来进行元素比较
|
|
|
|
// 定义一个 functor,用来进行元素比较
|
|
|
|
class value_compare : public binary_function <value_type, value_type, bool>
|
|
|
|
class value_compare : public binary_function<value_type, value_type, bool>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
friend class multimap<Key, T, Compare>;
|
|
|
|
friend class multimap<Key, T, Compare>;
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
Compare comp;
|
|
|
|
Compare comp; // 比较函数对象
|
|
|
|
value_compare(Compare c) : comp(c) {}
|
|
|
|
value_compare(Compare c) : comp(c) {}
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
bool operator()(const value_type& lhs, const value_type& rhs) const
|
|
|
|
bool operator()(const value_type& lhs, const value_type& rhs) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return comp(lhs.first, rhs.first);
|
|
|
|
return comp(lhs.first, rhs.first); // 比较两个元素的键
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
// 用 mystl::rb_tree 作为底层机制
|
|
|
|
// 以 mystl::rb_tree 作为底层机制
|
|
|
|
typedef mystl::rb_tree<value_type, key_compare> base_type;
|
|
|
|
typedef mystl::rb_tree<value_type, key_compare> base_type; // 红黑树类型
|
|
|
|
base_type tree_;
|
|
|
|
base_type tree_; // 红黑树实例
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
// 使用 rb_tree 的型别
|
|
|
|
// 使用 rb_tree 的型别
|
|
|
@ -347,203 +378,234 @@ public:
|
|
|
|
typedef typename base_type::difference_type difference_type;
|
|
|
|
typedef typename base_type::difference_type difference_type;
|
|
|
|
typedef typename base_type::allocator_type allocator_type;
|
|
|
|
typedef typename base_type::allocator_type allocator_type;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
|
|
|
multimap() = default; // 默认构造函数
|
|
|
|
multimap() = default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
template <class InputIterator>
|
|
|
|
multimap(InputIterator first, InputIterator last)
|
|
|
|
multimap(InputIterator first, InputIterator last)
|
|
|
|
:tree_()
|
|
|
|
:tree_()
|
|
|
|
{ tree_.insert_multi(first, last); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.insert_multi(first, last);
|
|
|
|
|
|
|
|
} // 构造函数,使用迭代器范围插入元素
|
|
|
|
|
|
|
|
|
|
|
|
multimap(std::initializer_list<value_type> ilist)
|
|
|
|
multimap(std::initializer_list<value_type> ilist)
|
|
|
|
:tree_()
|
|
|
|
:tree_()
|
|
|
|
{ tree_.insert_multi(ilist.begin(), ilist.end()); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.insert_multi(ilist.begin(), ilist.end());
|
|
|
|
|
|
|
|
} // 构造函数,使用初始化列表插入元素
|
|
|
|
|
|
|
|
|
|
|
|
multimap(const multimap& rhs)
|
|
|
|
multimap(const multimap& rhs)
|
|
|
|
:tree_(rhs.tree_)
|
|
|
|
:tree_(rhs.tree_)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // 复制构造函数
|
|
|
|
multimap(multimap&& rhs) noexcept
|
|
|
|
multimap(multimap&& rhs) noexcept
|
|
|
|
:tree_(mystl::move(rhs.tree_))
|
|
|
|
:tree_(mystl::move(rhs.tree_))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} // 移动构造函数
|
|
|
|
|
|
|
|
|
|
|
|
multimap& operator=(const multimap& rhs)
|
|
|
|
multimap& operator=(const multimap& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_ = rhs.tree_;
|
|
|
|
tree_ = rhs.tree_;
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 复制赋值运算符
|
|
|
|
multimap& operator=(multimap&& rhs)
|
|
|
|
multimap& operator=(multimap&& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_ = mystl::move(rhs.tree_);
|
|
|
|
tree_ = mystl::move(rhs.tree_);
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 移动赋值运算符
|
|
|
|
|
|
|
|
|
|
|
|
multimap& operator=(std::initializer_list<value_type> ilist)
|
|
|
|
multimap& operator=(std::initializer_list<value_type> ilist)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_.clear();
|
|
|
|
tree_.clear();
|
|
|
|
tree_.insert_multi(ilist.begin(), ilist.end());
|
|
|
|
tree_.insert_multi(ilist.begin(), ilist.end());
|
|
|
|
return *this;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} // 从初始化列表赋值
|
|
|
|
|
|
|
|
|
|
|
|
// 相关接口
|
|
|
|
// 相关接口
|
|
|
|
|
|
|
|
key_compare key_comp() const { return tree_.key_comp(); } // 获取键的比较函数
|
|
|
|
key_compare key_comp() const { return tree_.key_comp(); }
|
|
|
|
value_compare value_comp() const { return value_compare(tree_.key_comp()); } // 获取值的比较函数
|
|
|
|
value_compare value_comp() const { return value_compare(tree_.key_comp()); }
|
|
|
|
allocator_type get_allocator() const { return tree_.get_allocator(); } // 获取分配器
|
|
|
|
allocator_type get_allocator() const { return tree_.get_allocator(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 迭代器相关
|
|
|
|
// 迭代器相关
|
|
|
|
|
|
|
|
iterator begin() noexcept { return tree_.begin(); } // 返回开始的迭代器
|
|
|
|
iterator begin() noexcept
|
|
|
|
const_iterator begin() const noexcept { return tree_.begin(); } // 返回开始的常量迭代器
|
|
|
|
{ return tree_.begin(); }
|
|
|
|
iterator end() noexcept { return tree_.end(); } // 返回末尾的迭代器
|
|
|
|
const_iterator begin() const noexcept
|
|
|
|
const_iterator end() const noexcept { return tree_.end(); } // 返回末尾的常量迭代器
|
|
|
|
{ return tree_.begin(); }
|
|
|
|
|
|
|
|
iterator end() noexcept
|
|
|
|
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } // 返回反向开始的迭代器
|
|
|
|
{ return tree_.end(); }
|
|
|
|
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } // 返回反向开始的常量迭代器
|
|
|
|
const_iterator end() const noexcept
|
|
|
|
reverse_iterator rend() noexcept { return reverse_iterator(begin()); } // 返回反向末尾的迭代器
|
|
|
|
{ return tree_.end(); }
|
|
|
|
const_reverse_iterator rend() const noexcept {
|
|
|
|
|
|
|
|
return const_reverse
|
|
|
|
reverse_iterator rbegin() noexcept
|
|
|
|
iterator rend() const noexcept { return const_reverse_iterator(begin()); } // 返回反向末尾的常量迭代器
|
|
|
|
{ return reverse_iterator(end()); }
|
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const noexcept
|
|
|
|
const_iterator cbegin() const noexcept { return begin(); } // 返回开始的常量迭代器
|
|
|
|
{ return const_reverse_iterator(end()); }
|
|
|
|
const_iterator cend() const noexcept { return end(); } // 返回末尾的常量迭代器
|
|
|
|
reverse_iterator rend() noexcept
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return rbegin(); } // 返回反向开始的常量迭代器
|
|
|
|
{ return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator crend() const noexcept { return rend(); } // 返回反向末尾的常量迭代器
|
|
|
|
const_reverse_iterator rend() const noexcept
|
|
|
|
|
|
|
|
{ return const_reverse_iterator(begin()); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator cbegin() const noexcept
|
|
|
|
|
|
|
|
{ return begin(); }
|
|
|
|
|
|
|
|
const_iterator cend() const noexcept
|
|
|
|
|
|
|
|
{ return end(); }
|
|
|
|
|
|
|
|
const_reverse_iterator crbegin() const noexcept
|
|
|
|
|
|
|
|
{ return rbegin(); }
|
|
|
|
|
|
|
|
const_reverse_iterator crend() const noexcept
|
|
|
|
|
|
|
|
{ return rend(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 容量相关
|
|
|
|
// 容量相关
|
|
|
|
bool empty() const noexcept { return tree_.empty(); }
|
|
|
|
bool empty() const noexcept { return tree_.empty(); } // 检查是否为空
|
|
|
|
size_type size() const noexcept { return tree_.size(); }
|
|
|
|
size_type size() const noexcept { return tree_.size(); } // 获取大小
|
|
|
|
size_type max_size() const noexcept { return tree_.max_size(); }
|
|
|
|
size_type max_size() const noexcept { return tree_.max_size(); } // 获取最大容量
|
|
|
|
|
|
|
|
|
|
|
|
// 插入删除操作
|
|
|
|
// 插入删除操作
|
|
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
template <class ...Args>
|
|
|
|
iterator emplace(Args&& ...args)
|
|
|
|
iterator emplace(Args&& ...args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.emplace_multi(mystl::forward<Args>(args)...);
|
|
|
|
return tree_.emplace_multi(mystl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
} // 原地构造元素并插入
|
|
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
template <class ...Args>
|
|
|
|
iterator emplace_hint(iterator hint, Args&& ...args)
|
|
|
|
iterator emplace_hint(iterator hint, Args&& ...args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.emplace_multi_use_hint(hint, mystl::forward<Args>(args)...);
|
|
|
|
return tree_.emplace_multi_use_hint(hint, mystl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
} // 原地构造元素并插入,使用 hint 提示位置
|
|
|
|
|
|
|
|
|
|
|
|
iterator insert(const value_type& value)
|
|
|
|
iterator insert(const value_type & value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_multi(value);
|
|
|
|
return tree_.insert_multi(value);
|
|
|
|
}
|
|
|
|
} // 插入元素
|
|
|
|
iterator insert(value_type&& value)
|
|
|
|
iterator insert(value_type && value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_multi(mystl::move(value));
|
|
|
|
return tree_.insert_multi(mystl::move(value));
|
|
|
|
}
|
|
|
|
} // 插入移动构造的元素
|
|
|
|
|
|
|
|
|
|
|
|
iterator insert(iterator hint, const value_type& value)
|
|
|
|
iterator insert(iterator hint, const value_type & value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_multi(hint, value);
|
|
|
|
return tree_.insert_multi(hint, value);
|
|
|
|
}
|
|
|
|
} // 插入元素,并使用 hint 提示位置
|
|
|
|
iterator insert(iterator hint, value_type&& value)
|
|
|
|
iterator insert(iterator hint, value_type && value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return tree_.insert_multi(hint, mystl::move(value));
|
|
|
|
return tree_.insert_multi(hint, mystl::move(value));
|
|
|
|
}
|
|
|
|
} // 插入移动构造的元素,并使用 hint 提示位置
|
|
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
template <class InputIterator>
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tree_.insert_multi(first, last);
|
|
|
|
tree_.insert_multi(first, last);
|
|
|
|
}
|
|
|
|
} // 插入迭代器区间内的所有元素
|
|
|
|
|
|
|
|
|
|
|
|
void erase(iterator position) { tree_.erase(position); }
|
|
|
|
void erase(iterator position)
|
|
|
|
size_type erase(const key_type& key) { return tree_.erase_multi(key); }
|
|
|
|
{
|
|
|
|
void erase(iterator first, iterator last) { tree_.erase(first, last); }
|
|
|
|
tree_.erase(position);
|
|
|
|
|
|
|
|
} // 擦除指定位置的元素
|
|
|
|
|
|
|
|
|
|
|
|
void clear() { tree_.clear(); }
|
|
|
|
size_type erase(const key_type & key)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.erase_multi(key);
|
|
|
|
|
|
|
|
} // 擦除指定键的所有元素,并返回擦除的元素数量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void erase(iterator first, iterator last)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.erase(first, last);
|
|
|
|
|
|
|
|
} // 擦除[first, last)区间内的元素
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.clear();
|
|
|
|
|
|
|
|
} // 清空 multimap
|
|
|
|
|
|
|
|
|
|
|
|
// multimap 相关操作
|
|
|
|
// multimap 相关操作
|
|
|
|
|
|
|
|
iterator find(const key_type & key)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.find(key);
|
|
|
|
|
|
|
|
} // 查找键对应的迭代器,如果不存在则返回 end()
|
|
|
|
|
|
|
|
|
|
|
|
iterator find(const key_type& key) { return tree_.find(key); }
|
|
|
|
const_iterator find(const key_type & key) const
|
|
|
|
const_iterator find(const key_type& key) const { return tree_.find(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.find(key);
|
|
|
|
|
|
|
|
} // 查找键对应的迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
size_type count(const key_type& key) const { return tree_.count_multi(key); }
|
|
|
|
size_type count(const key_type & key) const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.count_multi(key);
|
|
|
|
|
|
|
|
} // 返回指定键的元素数量
|
|
|
|
|
|
|
|
|
|
|
|
iterator lower_bound(const key_type& key) { return tree_.lower_bound(key); }
|
|
|
|
iterator lower_bound(const key_type & key)
|
|
|
|
const_iterator lower_bound(const key_type& key) const { return tree_.lower_bound(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.lower_bound(key);
|
|
|
|
|
|
|
|
} // 返回指向键值大于或等于给定键的首个迭代器
|
|
|
|
|
|
|
|
|
|
|
|
iterator upper_bound(const key_type& key) { return tree_.upper_bound(key); }
|
|
|
|
const_iterator lower_bound(const key_type & key) const
|
|
|
|
const_iterator upper_bound(const key_type& key) const { return tree_.upper_bound(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.lower_bound(key);
|
|
|
|
|
|
|
|
} // 返回指向键值大于或等于给定键的首个迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator upper_bound(const key_type & key)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.upper_bound(key);
|
|
|
|
|
|
|
|
} // 返回指向键值大于给定键的首个迭代器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator upper_bound(const key_type & key) const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.upper_bound(key);
|
|
|
|
|
|
|
|
} // 返回指向键值大于给定键的首个迭代器(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
pair<iterator, iterator>
|
|
|
|
pair<iterator, iterator>
|
|
|
|
equal_range(const key_type& key)
|
|
|
|
equal_range(const key_type & key)
|
|
|
|
{ return tree_.equal_range_multi(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.equal_range_multi(key);
|
|
|
|
|
|
|
|
} // 返回指定键值的范围
|
|
|
|
|
|
|
|
|
|
|
|
pair<const_iterator, const_iterator>
|
|
|
|
pair<const_iterator, const_iterator>
|
|
|
|
equal_range(const key_type& key) const
|
|
|
|
equal_range(const key_type & key) const
|
|
|
|
{ return tree_.equal_range_multi(key); }
|
|
|
|
{
|
|
|
|
|
|
|
|
return tree_.equal_range_multi(key);
|
|
|
|
|
|
|
|
} // 返回指定键值的范围(常量版本)
|
|
|
|
|
|
|
|
|
|
|
|
void swap(multimap& rhs) noexcept
|
|
|
|
void swap(multimap & rhs) noexcept
|
|
|
|
{ tree_.swap(rhs.tree_); }
|
|
|
|
{
|
|
|
|
|
|
|
|
tree_.swap(rhs.tree_);
|
|
|
|
|
|
|
|
} // 交换两个 multimap 对象的内容
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
friend bool operator==(const multimap& lhs, const multimap& rhs) { return lhs.tree_ == rhs.tree_; }
|
|
|
|
friend bool operator==(const multimap & lhs, const multimap & rhs)
|
|
|
|
friend bool operator< (const multimap& lhs, const multimap& rhs) { return lhs.tree_ < rhs.tree_; }
|
|
|
|
{
|
|
|
|
};
|
|
|
|
return lhs.tree_ == rhs.tree_;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 比较两个 multimap 是否相等
|
|
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
friend bool operator<(const multimap & lhs, const multimap & rhs)
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
{
|
|
|
|
bool operator==(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
return lhs.tree_ < rhs.tree_;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return lhs == rhs;
|
|
|
|
// 比较两个 multimap 的字典序
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
// 其他比较运算符的重载...
|
|
|
|
bool operator<(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
};
|
|
|
|
{
|
|
|
|
|
|
|
|
return lhs < rhs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
// 重载比较操作符,允许直接比较两个 multimap 对象...
|
|
|
|
bool operator!=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
{
|
|
|
|
bool operator!=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
bool operator>(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
bool operator>(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return rhs < lhs;
|
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
bool operator<=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
bool operator<=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return !(rhs < lhs);
|
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
bool operator>=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
bool operator>=(const multimap<Key, T, Compare>& lhs, const multimap<Key, T, Compare>& rhs)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return !(lhs < rhs);
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
// 重载 mystl 的 swap 函数,使得 std::swap 可以直接使用
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
template <class Key, class T, class Compare>
|
|
|
|
void swap(multimap<Key, T, Compare>& lhs, multimap<Key, T, Compare>& rhs) noexcept
|
|
|
|
void swap(multimap<Key, T, Compare>& lhs, multimap<Key, T, Compare>& rhs) noexcept
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mystl
|
|
|
|
} // namespace mystl
|
|
|
|
#endif // !MYTINYSTL_MAP_H_
|
|
|
|
#endif // !MYTINYSTL_MAP_H_
|
|
|
|
|
|
|
|
|
|
|
|