更改 C++ 的默认整数值字典 TryGetValue() 方法?
我担心这个问题的答案简短而令人失望,但可惜我还是要问。 在 C++ 字典方法 TryGetValue() 中,当键不存在时,是否有任何方法可以更改整数返回的默认值(例如改为 -1)?问题是 0 是默认值,这不合适,因为 0 值在我的程序上下文中有意义。
如果不是,ContainsKey() 方法会慢很多吗?或者它是分裂头发,没什么可担心的,因为我很可能别无选择。
非常感谢
PS,我不需要执行任何哈希函数(尽管这可能在字典的实现中!),也没有对我的收藏的任何特定排序,我只想尽可能快地查找和添加。 Dictionary 是一个合理的选择吗?
Quick question which I fear has a short and disappointing answer but alas I shall ask anyway..
In the C++ Dictionary method TryGetValue() is there any way to change the default value that will be returned for an integer (to -1 instead for example) when the key is not present? The problem is that 0 is the default and this is not suitable because a value of 0 would make sense in the context of my program.
If not, is the ContainsKey() method that much slower? Or is it splitting hairs and nothing to worry about seeing as in all likelihood I have no choice..
Many thanks
PS I don't need to perform any hashing function (though this might be in the implementation for Dictionary anyway!), nor have any particular ordering to my collection, I just want lookup and adding to be as fast as possible. Is Dictionary a sound choice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不创建一个继承自
Dictionary(Of TKey, TValue)
并覆盖TryGetValue
函数。然后你可以使用你自己的类,它的行为就像你希望的那样......Why don't you create a class that inherits from
Dictionary(Of TKey, TValue)
and override theTryGetValue
function. You could then use your own class and it would behave just as you would want it to...