是否可以在 STL 映射中使用自定义类代替 std::pair ?
这可能吗?
#include <map>
class Example {
private:
std::map<std::string, std::string, less<std::string>,
std::allocator< CustomPair<std::string, std::string> > > myMap;
};
在上面的示例中,CustomPair 将是一个包含键和值的模板类。 如果可能的话,是不是那么简单,或者有什么我应该注意的吗?
Is this possible?
#include <map>
class Example {
private:
std::map<std::string, std::string, less<std::string>,
std::allocator< CustomPair<std::string, std::string> > > myMap;
};
In the example above, CustomPair would be a template class holding a key and value. If this is possible, is it that simple or is there anything I should look out for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
人们只能推测你的真正意图是什么,所以我假设你已经有一个包含键和值的类。 在这种情况下,带有自定义比较的 std::set 可能比 std::map 更好。
然后,您需要提供一个比较,该比较仅比较类的关键部分,并且只要对象位于集合中,关键部分就必须是 const (不随时间变化)。
正如评论中提到的,集合的元素只能作为 const 访问,因此如果您想更改此类元素的值,您需要 const_cast 写入访问或声明成员可变。
在另一个答案中iain提出了另一个非常好的建议。 如果您很少插入到容器中,并且主要访问容器来搜索元素,那么排序的 std::vector 和 std::binary_search 是集合的非常有效的替代方案。
One can only speculate what your real intent is here, so I assume you already have a class that contains both key and value. In that case std::set with a custom comparison may be a better choice than a std::map.
You then need to provide a comparison that will only compare the key part of your class and the key part must be const (not change over time) as long as the object is in the set.
As mentioned in the comment the elements of a set are only accessable as consts, so if you want to change the value of a such element you need to const_cast the write access or declare the member mutable.
In another answer iain made another very good suggestion. If you rarely insert into the container and mostly access the container searching for elements then a sorted std::vector and std::binary_search are a very effective alternative to the set.
我更有可能使用 std::set。
I would be more likely to use std::set.
我要么使用lothar所描述的集合,要么使用排序的
std::vector
,如“有效的STL”第23章中所述:“考虑用排序向量替换关联容器”。这样做的原因是,带有自定义比较器的排序向量的
std::binary_search
几乎与地图查找一样快,有时甚至更快,而迭代则快得多。 不过,插入操作的成本更高(您必须在每次插入后调用排序)。 不过,许多地图用例的插入频率非常低。向量比集合更灵活。
我用这种方法替换了 2000 个复杂对象(由 int 索引)的映射,在服务器类系统上迭代和处理映射中的每个对象从 50 秒缩短到不到 5 秒。 地图查找时间没有明显差异。
I would either use a set as described by lothar or use an sorted
std::vector
as described in "Effective STL" chapter 23: "Consider replacing associative containers with sorted vectors".The rational for this is that a
std::binary_search
of a sorted vector with a custom comparitor is nearly as fast and sometimes faster than a map lookup and iteration is much faster. The insert operations are more expensive though (you have to call sort after each insert). A lot of map use cases insert very infrequently though.The vector would be more flexibility than the set.
I replaced a map of 2000 complex objects (indexed by int) with this approach, iteration and processing every object in the map went from 50 seconds to less than 5 on a server class system. There was no noticeable difference for map lookups times.
我认为你可以做到这一点,但不会获得所需的效果,因为
std::allocator
的使用将通过rebind
完成,从而覆盖您选择的CustomPair
。 事实上,您放在那里的类型可能并不重要,STL 函数都会忽略它。 至少他们中的一些人肯定会这样做,但我不确定所有人都会这样做。 严格来说,这几乎肯定取决于实现。 我不知道标准是怎么说的。I think you can do it but will not get the desired effect, because the use of
std::allocator
will be done viarebind<std::pair>
, thus overriding your selection ofCustomPair
. In fact, it probably doesn't matter what type you put there, the STL functions will ignore it. At least some of them will definitely do this, but I'm not sure all will. Strictly speaking this is almost certainly implementation dependent. I don't know what the standard says.