3#include "IComparable.h"
12 template <
typename TKey,
typename TVal>
18 template <
typename TVal>
19 using StrDictionary = Dictionary<std::wstring, TVal>;
24 template <
typename TKey,
typename TVal>
32 std::shared_ptr<std::map<TKey, TVal>> _pMap;
39 : _pMap(std::make_shared<std::map<TKey, TVal>>())
46 Dictionary(std::initializer_list<std::pair<const TKey, TVal>> list)
47 : _pMap(std::make_shared<std::map<TKey, TVal>>(list))
56 return this->_pMap->begin();
64 return this->_pMap->end();
72 return this->_pMap->rbegin();
80 return this->_pMap->rend();
89 return this->_pMap->at(key);
97 return this->_pMap == other._pMap;
105 return (
int)this->_pMap->size();
113 return this->_pMap->empty();
120 auto Add(
const TKey &key,
const TVal &value)
const
122 this->_pMap->insert(std::make_pair(key, value));
132 return this->_pMap->count(key);
141 for (
const auto &pair : *this->_pMap) {
142 if (pair.second == value) {
155 this->_pMap->erase(key);
163 this->_pMap->clear();
172 dic._pMap->insert(this->_pMap->begin(), this->_pMap->end());
字典类,内部维护了一个指向std::map的智能指针
Definition Dictionary.h:27
void Clear() const
清空字典
Definition Dictionary.h:161
auto Add(const TKey &key, const TVal &value) const
添加键值对到字典
Definition Dictionary.h:120
bool IsEmpty() const
字典是否为空
Definition Dictionary.h:111
Dictionary(std::initializer_list< std::pair< const TKey, TVal > > list)
使用初始化列表
Definition Dictionary.h:46
bool Equals(const Dictionary &other) const
判断是否为同一个字典
Definition Dictionary.h:95
auto & operator[](const TKey &key) const
获取或设置值
Definition Dictionary.h:87
std::map< TKey, TVal > & GetStdMap() const
获取字典内部维护的std::map
Definition Dictionary.h:179
bool ContainsKey(const TKey &key) const
是否存在某个键值
Definition Dictionary.h:130
Dictionary()
初始化字典
Definition Dictionary.h:38
std::wstring ToString() const
获取描述当前对象的字符串
Definition Dictionary.h:187
int Count() const
获取键值对个数
Definition Dictionary.h:103
auto end() const
正向迭代器结束
Definition Dictionary.h:62
auto begin() const
正向迭代器开始
Definition Dictionary.h:54
bool ContainsValue(const TVal &value) const
遍历字典,查询是否存在某个值
Definition Dictionary.h:139
auto rbegin() const
反向迭代器开始
Definition Dictionary.h:70
auto rend() const
反向迭代器结束
Definition Dictionary.h:78
Dictionary Copy() const
复制当前字典
Definition Dictionary.h:169
void Remove(const TKey &key) const
移除指定键值对
Definition Dictionary.h:153
相等性比较接口
Definition IComparable.h:14
为支持ToString方法的类提供统一接口
Definition IToString.h:13
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:113