- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个哈希表类。我面临的 atm 问题是如何正确地重载方括号运算符,以便从哈希表中获取键的值与将键设置为值是有区别的。
到目前为止,这个类是这样的:
template <typename K, typename V>
class HashTable {
typedef pair<K, V> KeyVal;
avl_tree <KeyVal> **TABLE;
unsigned TABLESIZE;
public:
HashTable( const unsigned & );
V& operator [] ( const K& ); //Setter
const V& operator [](const K&) const; //Getter
typedef unsigned (*hashtype)(const K&);
static hashtype Hash;
~HashTable();
};
这是每个括号重载的实现:
template <typename K, typename V>
V& HashTable<K, V>::operator [] ( const K& ret ) {
unsigned index = HashTable<K, V>::Hash(ret) % TABLESIZE;
avl_tree <KeyVal> *ptr = AVL_TREE::find(TABLE[index], KeyVal(ret, 0));
if ( ptr == None ) ptr = (TABLE[index] = AVL_TREE::insert(TABLE[index], KeyVal(ret, 0)));
return ptr->data.second;
}
template <typename K, typename V>
const V& HashTable<K, V>::operator [](const K& ret) const {
avl_tree <KeyVal> *ptr = AVL_TREE::find(TABLE[HashTable<K, V>::Hash(ret) % TABLESIZE], KeyVal(ret, 0));
if (ptr == None) throw "Exception: [KeyError] Key not found exception.";
return ptr->data.second;
}
现在如果我这样做:
cout << table["hash"] << "\n"; //table declared as type HashTable<std::string, int>
我的输出为 0,但我希望它使用重载方括号的 getter 实现;即这应该抛出异常。我该怎么做?
最佳答案
处理这种情况的通常方法是让 operator[]
返回一个代理。
然后,对于代理重载 operator T
,大致与您在上面完成 const
重载一样。重载 operator=
就像你的非常量版本一样。
template <typename K, typename V>
class HashTable {
typedef pair<K, V> KeyVal;
avl_tree <KeyVal> **TABLE;
unsigned TABLESIZE;
template <class K, class V>
class proxy {
HashTable<K, V> &h;
K key;
public:
proxy(HashTable<K, V> &h, K key) : h(h), key(key) {}
operator V() const {
auto pos = h.find(key);
if (pos) return *pos;
else throw not_present();
}
proxy &operator=(V const &value) {
h.set(key, value);
return *this;
}
};
public:
HashTable( const unsigned & );
proxy operator [] ( const K& k) { return proxy(*this, k); }
typedef unsigned (*hashtype)(const K&);
static hashtype Hash;
~HashTable();
};
当你使用这个时,你基本上有两种情况:
some_hash_table[some_key] = some_value;
value_type v = some_hash_table[some_key];
在这两种情况下,some_hash_table[some_key]
都会返回 proxy
的实例。在第一种情况下,您将 分配给 代理对象,以便调用代理的 operator=
,将其传递给 some_value
,因此 some_value
被添加到以 key
作为键的表中。
在第二种情况下,您试图将类型为 proxy
的对象分配给类型为 value_type
的变量。显然不能直接赋值——但是 proxy::operator V
返回底层 Hashtable
值类型的对象——所以编译器调用它来生成可以分配给 v
的值。反过来,检查表中是否存在正确的键,如果不存在则抛出异常。
关于c++ - 为哈希表 get 和 set 正确重载 [bracket] 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670530/
我的主目录中有大量文件( ~ ),每次我打开 Brackets 时,它都会尝试加载和索引那里的所有文件。最终,在卡住一分钟后,它抛出错误: Error Indexing Files: The maxi
我决定试用文本编辑器 [括号]。我只是想知道有没有办法使用括号在所有打开的文档中查找和替换? 我可以看到查找和替换选项,但我必须在多个页面上搜索大量内容。 我似乎无法从任何下拉菜单中找到快捷方式或选项
有没有办法在 Adobe-Brackets 的侧栏中打开多个项目文件夹?我尝试过谷歌搜索,但没有找到针对这个问题的任何答案。当我尝试打开辅助项目文件夹时,侧边栏将关闭当前打开的项目文件夹并将其替换
这是我的情况:我的 table 上有两台电脑,我想用其中一台开发网页,用另一台做实时预览。 Brackets 中有我可以执行此操作的选项吗?我在 native 做实时预览的时候尝试用url中出现的端口
我正在为 Brackets 使用 CSSLint 扩展.有没有办法禁用诸如 box-model 之类的特定警告和 @bulletproof-font-face ?我为讨论找到的最佳匹配是 here .
由于 Brackets 似乎启动了一个新的 Chrome 实例,(如何)我可以让它用 --disable-web-security 启动 Chrome选项,以便在我的本地机器上开发客户端和服务器时解决
我正在构建一个导航栏,需要在按钮之前和按钮之后插入一个括号,以便在悬停和选择按钮时突出显示该按钮,如下例所示 [ Button 1 ] button 2 button 3 but
我已经下载了适用于 Node 的 Angular CLI 6.0.7 并且正在使用它,按照教程等学习尽可能多的知识。 我有一个问题是数据绑定(bind)。好像我想绑定(bind)组件成员变量的值,比如
我在写MYSQL的查询时很困惑。 我想要实现的目标 CREATE TABLE paypal_ipn_orders (`id` int, `name` varchar(15), `neighbo
我需要为 java 源代码抄袭检测器编写一个词法分析器。这是我想要实现的示例。 //Java code Tokens: public
我有一个维基百科转储,正在努力寻找合适的正则表达式模式来删除表达式中的双方括号。这是表达式的示例: line = '是除草剂橙 (HO) 和 Agent LNX 的代号组合,LNX 是 [[美国武装部
两天前,我开始研究代码解析器,但遇到了困难。 如何用不在括号内的逗号分隔字符串,让我告诉你我的意思: 我要解析这个字符串: one, two, three, (four, (five, six), (
我需要一些帮助来挽救我的一天(或我的夜晚)。我想匹配: 任意数量的数字 用圆括号“()”括起来[括号中只包含数字] 如果右括号“)”是字符串中的最后一个字符。 这是我想出的代码: // this ho
我当前的 .eslintrc.js 文件为 eslint-plugin-vue 设置了这些选项插件: "vue/html-closing-bracket-newline": ["error", {
我想将括号中编辑的字体大小从默认大小更改为 16pt。 我去查看--> 主题--> 将字体大小编辑为 16pt 或其他大小,但它没有改变。 最佳答案 试试键盘快捷键 Ctrl++ 关于adobe-br
Brackets 安装了两个主题,“Backets Light(默认)”和“Backets Dark”。要查看它们,请转到查看 |主题... 我真的很喜欢“Brackets Dark”主题,我想为 G
我想将所有工作文件保存在 Brackets 中的一个单独选项卡中,因为我发现在“工作文件”下的 LHS 面板中打开文件有点困难。有没有办法做到这一点? 最佳答案 Tabs for Brackets e
如何在 Adobe Brackets 中启用拖放文本的功能? 例如,当我在 Sublime Text 中选择文本并拖动它时,它会移动。我怎样才能在 Bracket 中做同样的事情? 最佳答案 您正
在括号中输入光标后,键入会自动创建: 但是,当您按Enter键时,只需将结束标记放在下一行: 要开始添加代码,我需要按(输入* 2),向上箭头,制表符(太多步骤)。 如何自动执行此操作,以便按Ente
我在一个文件中因未声明的函数和从另一个文件引用的变量而收到 JSLint 错误。括号是否有配置/菜单来删除这些同时保留其他 linting 错误? 最佳答案 每当您引用一个它在文件中看不到任何声明的标
我是一名优秀的程序员,十分优秀!