25#include <unordered_map>
40template<
typename Key,
typename Value>
43 using vector_type = std::vector<std::pair<Key, Value>>;
44 using map_type = std::unordered_map<Key, std::size_t>;
52 using iterator =
typename vector_type::iterator;
53 using const_iterator =
typename vector_type::const_iterator;
55 iterator begin() {
return items_.begin(); }
56 iterator end() {
return items_.end(); }
57 const_iterator begin()
const {
return items_.begin(); }
58 const_iterator end()
const {
return items_.end(); }
59 const_iterator cbegin()
const {
return items_.cbegin(); }
60 const_iterator cend()
const {
return items_.cend(); }
66 return items_.empty();
70 std::size_t
size() const noexcept {
78 auto it = index_map_.find(key);
79 if (it == index_map_.end())
80 throw std::out_of_range(
"OrderedMap::at: key not found");
81 return items_[it->second].second;
86 auto it = index_map_.find(key);
87 if (it == index_map_.end())
88 throw std::out_of_range(
"OrderedMap::at: key not found");
89 return items_[it->second].second;
92 Value& operator[](
const Key& key) {
93 auto it = index_map_.find(key);
94 if (it != index_map_.end()) {
95 return items_[it->second].second;
98 index_map_[key] = items_.size();
99 items_.emplace_back(key, Value{});
100 return items_.back().second;
107 auto it = index_map_.find(key);
108 if (it != index_map_.end()) {
110 items_[it->second].second = value;
113 index_map_[key] = items_.size();
114 items_.emplace_back(key, value);
119 void insert(std::pair<Key, Value>&& pair) {
120 insert(pair.first, std::move(pair.second));
126 iterator
find(
const Key& key) {
127 auto it = index_map_.find(key);
128 if (it == index_map_.end())
130 return items_.begin() + it->second;
134 const_iterator
find(
const Key& key)
const {
135 auto it = index_map_.find(key);
136 if (it == index_map_.end())
138 return items_.begin() + it->second;
143 return index_map_.find(key) != index_map_.end();
155 std::vector<Key>
keys()
const {
156 std::vector<Key> result;
157 result.reserve(items_.size());
158 for (
const auto& pair : items_) {
159 result.push_back(pair.first);
166 std::vector<Value> result;
167 result.reserve(items_.size());
168 for (
const auto& pair : items_) {
169 result.push_back(pair.second);
175 const vector_type&
items()
const {
return items_; }
有序映射类,保持键值对的插入顺序
定义 OrderedMap.hpp:41
void clear()
清空映射
定义 OrderedMap.hpp:148
const_iterator find(const Key &key) const
查找键对应的迭代器(常量版本)
定义 OrderedMap.hpp:134
Value & at(const Key &key)
获取键对应的值
定义 OrderedMap.hpp:77
void insert(const Key &key, const Value &value)
插入或更新键值对
定义 OrderedMap.hpp:106
std::size_t size() const noexcept
获取映射中键值对的数量
定义 OrderedMap.hpp:70
void insert(std::pair< Key, Value > &&pair)
插入或更新键值对(移动语义)
定义 OrderedMap.hpp:119
bool empty() const noexcept
检查映射是否为空
定义 OrderedMap.hpp:65
iterator find(const Key &key)
查找键对应的迭代器
定义 OrderedMap.hpp:126
const Value & at(const Key &key) const
获取键对应的值(常量版本)
定义 OrderedMap.hpp:85
std::vector< Value > values() const
获取所有值
定义 OrderedMap.hpp:165
std::vector< Key > keys() const
获取所有键
定义 OrderedMap.hpp:155
const vector_type & items() const
获取所有键值对
定义 OrderedMap.hpp:175
bool contains(const Key &key) const
检查映射是否包含指定键
定义 OrderedMap.hpp:142