容器的访问和分配是通过同一个操作员进行的吗?
我创建了一个用于通用弱类型数据的容器,可以通过下标运算符访问该数据。
std::map
容器允许通过运算符进行数据访问和元素插入,而 std::vector
我认为不允许。
最好的(C++ 风格)方法是什么?我应该允许通过下标运算符进行分配还是有一个单独的插入方法?
编辑
我应该说,我不是问我是否应该使用矢量或地图,我只是想知道人们对以这种方式组合访问和插入的想法。
I have created a container for generic, weak-type data which is accessible through the subscript operator.
The std::map
container allows both data access and element insertion through the operator, whereas std::vector
I think doesn't.
What is the best (C++ style) way to proceed? Should I allow allocation through the subscript operator or have a separate insert method?
EDIT
I should say, I'm not asking if I should use vector or map, I just wanted to know what people thought about accessing and inserting being combined in this way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于向量:下标表示法不会插入 - 它会覆盖。
本文的其余部分从 有效 STL 的第 1-5 项中提取信息。
如果您事先知道数据的范围 - 并且大小是固定的 - 并且您不会在其上方有数据的位置插入 - 那么您可以使用插入到向量中而不会产生令人不快的副作用。
然而,在一般情况下,当您进行临时操作时,向量插入会产生一些影响,例如向上移动成员以及在耗尽内存时加倍内存(这会导致从旧向量的对象到新向量中的位置的大量副本)。插入。向量是为了解数据的局部性特征而设计的。
向量带有一个插入成员函数......并且该函数对于大多数实现来说都非常聪明,因为它可以从您提供的迭代器中推断出优化。你就不能用这个吗?
如果您想临时插入数据,则应该使用列表。也许您可以使用列表来收集数据,然后在其最终确定后使用基于范围的插入或基于范围的构造函数填充向量?
In the case of Vectors: Subscript notation does not insert -- it overwrites.
This rest of this post distils the information from item 1-5 of Effective STL.
If you know the range of your data before hand -- and the size is fixed -- and you won't insert at locations which has data above it -- then you can use insert into vectors without unpleasant side-effects.
However in the general case vector insertions have implications such as shifting members upward and doubling memory when exhausted (which causes a flood of copies from the old vector's objects to locations in the new vector ) when you make ad hoc insertions. Vectors are designed for when you know the locality characteristics of your data..
Vectors come with an insert member function... and this function is very clever with most implementations in that it can infer optimizations from the iterators your supply. Can't you just use this ?
If you want to do ad-hoc insertions of data, you should use a list. Perhaps you can use a list to collect the data and then once its finalized populate a vector using the range based insert or range based constructor ?
这取决于你想要什么。如果您想使用数组之类的东西,映射可能会比向量慢得多。如果您要使用的索引是非顺序的并且您有大量索引,则映射非常有用。使用向量、对其进行排序并进行二分搜索来找到所需内容通常会更快。我已经使用这种方法来替换大量软件中的地图,但我仍然没有找到使用矢量执行此操作速度较慢的方法。
因此,IMO,std::vector 是更好的方法,尽管如果正确使用地图可能会很有用。
it depends what you want. A map can be significantly slower than a vector if you wish to use the thing like an array. A map is very helpful if the index you want to use is non-sequential and you have LOADS of them. Its usually quicker to just use a vector, sort it and do a binary search to find what you are after. I've used this method to replace maps in tonnes of software and I still haven't found something where it was slower to do this with a vector.
So, IMO, std::vector is the better way, though a map MIGHT be useful if you are using it properly.
当然,单独的插入方法。
std::map
上的operator[]
非常愚蠢,使代码难以阅读和调试。此外,如果您使用
operator[]
进行插入,则无法从 const 上下文访问数据(这将导致 un-const-cancer,甚至 - const-cancer的更邪恶的表弟)。Separate insert method, definitely. The
operator[]
onstd::map
is just stupid and makes the code hard to read and debug.Also you can't access data from a const context if you're using a
operator[]
to insert (which will lead to un-const-cancer, the even-more evil cousin of const-cancer).