简体   繁体   English

为什么 map 有 operator[] 而 set 没有?

[英]Why does map have operator[] but set does not?

std::map and std::set seem very similar to me (but in their use and in their description), so I do not understand why std::set does not implement its version of operator[] . std::mapstd::set似乎与我非常相似(但在它们的使用和描述中),所以我不明白为什么std::set没有实现它的operator[]版本。 I suspect it is linked to the fact that elements in a std::set are const , but even then why not implement an operator[] that either returns a const reference or creates a new element?我怀疑这与std::set中的元素是const的事实有关,但即便如此,为什么不实现返回const引用或创建新元素的operator[]呢?

Depending on the answer to this first question, would it be possible/a good idea to create a version a std::set that implements operator[] ?根据第一个问题的答案,创建一个实现operator[]std::set版本是否可能/一个好主意?

Well, std::map<Key, Val> maps Key to Val.好吧, std::map<Key, Val>将 Key映射到 Val。

That is, m[key] yields a reference to the val.也就是说, m[key]产生对 val 的引用。 You have the key, and want to find the associated value (or associate a value with that key).您拥有键,并希望找到关联的值(或将值与该键关联)。

In a std::set<Elem> , the element would be its own key.std::set<Elem>中,元素将是它自己的键。 So, the only thing you could get back would be the thing you already have.所以,你唯一能取回的就是你已经拥有的东西。 What would you use this operation for?你会用这个操作做什么?

A set is not for mapping one thing to another - that's what a map does.一组不是为了将一件事映射到另一件事——这就是map所做的。 A set is for recording whether or not an element belongs to some collection.集合用于记录一个元素是否属于某个集合。 So, the only sane thing to use it for is checking, given some element, whether or not that element is a member of the set.因此,使用它的唯一明智的事情是检查给定某个元素,该元素是否是集合的成员。 We do this with s.find(elem).= s.end() or, from c++20, s.contains(elem) .我们使用s.find(elem).= s.end()或 c++20 中的s.contains(elem)来做到这一点。

The fact that the set is described as std::set<Key, ...> may be a source of confusion - I suspect this is just because it's used for searching in the same way as the map key.该集合被描述为std::set<Key, ...>的事实可能会造成混淆 - 我怀疑这只是因为它用于搜索的方式与 map 密钥相同。

You could in principle choose to characterize a set as map<Elem, bool> , but unless you want to really store the bool (which would be wasteful), the element access and iterator semantics would be a bit hairy.您原则上可以选择将集合表征为map<Elem, bool> ,但除非您想真正存储 bool(这将是浪费),否则元素访问和迭代器语义会有点麻烦。 That is, it would be mathematically accurate and consistent, but either wasteful or complicated in implementation.也就是说,它在数学上是准确和一致的,但在实现上要么是浪费的,要么是复杂的。

In fact a map is an associated array only instead of integer indices it uses keys as indices.实际上,map 只是一个关联数组,而不是 integer 索引,它使用键作为索引。

As ordinary arrays have the subscript operator then and maps have a similar subscript operator.由于普通的 arrays 有下标运算符,那么地图也有类似的下标运算符。

On the other hand, sets are not associated arrays.另一方面,集合不关联 arrays。 In sets keys are their data.在集合中,键是他们的数据。 So a question arises what should an expression like this set[key] return?那么问题来了,像这样set[key]这样的表达式应该返回什么? There is no greate sense to return itself and moreover when the returned value may not be changed.返回本身并没有太大意义,而且当返回的值可能不会改变时。

It would not be very useful to use set if you want to know the key of an element, since a map would be more useful, which does have the function you require.如果您想知道元素的键,使用 set 不会很有用,因为 map 会更有用,它确实具有您需要的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM