指向集合或向量中对象的指针 - 这重要吗?

发布于 2024-09-29 10:47:31 字数 297 浏览 0 评论 0原文

刚刚遇到一种情况,我需要在 STL 容器中存储堆分配的指针(指向 B 类)。拥有私有容器的类(A 类)也会创建 B 的实例。A 类将能够为 A 的客户端返回指向 B 实例的 const 指针。

现在,这些指针是否存储在集合中是否重要?或者一个向量?我想到有一个集合只是为了验证没有存储重复项,但由于存储了地址,因此可以存储具有相同数据的两个 B 指针(除非我提供一个用于数据比较的比较类)。

对这个(相当模糊)主题有什么想法吗?替代方案的优点/缺点是什么? smart_pointers 值得研究吗?

有什么不懂的地方请追问,谢谢!

just came a across a situation where I needs to store heap-allocated pointers (to a class B) in an STL container. The class that owns the privately held container (class A) also creates the instances of B. Class A will be able to return a const pointers to B instances for clients of A.

Now, does it matter if these pointer are stored in a set or a vector? I thought of having a set just to verify that no duplicates are stored but since addresses are stored, two B pointers with the same data can be stored (unless I provide a comparison class for data comparison I presume).

Any thoughts on this (quite vague) subject? What are the pros/cons for the alternatives? Are smart_pointers something to look into?

Please ask me if anything imperative is unclear, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁吢 2024-10-06 10:47:32

将指针存储在标准容器中没有任何问题 - 无论是向量、集合、映射还是其他容器。您只需要知道谁拥有该内存并确保它被适当地释放。选择容器时,请选择最适合您需求的容器。矢量非常适合随机访问和附加,但不太适合插入容器中的其他位置。 list 可以很好地处理插入,但它不具有随机访问功能。集合确保容器中没有重复项并且已排序(尽管如果集合包含指针并且您不提供比较器函数,则排序不是很有用),而映射是一组键值对,因此排序和访问是通过键完成的。等等。每个容器都有其优点和缺点,哪种容器最适合特定情况完全取决于具体情况。

至于指针,同样,将指针放在容器中就可以了。您需要担心的问题是谁拥有内存,因此必须担心释放它。如果有一个明确的对象拥有特定指针所指向的内容,那么它可能应该是释放它的对象。如果本质上是容器拥有内存,那么您需要确保在容器被销毁之前删除容器中的所有指针。

如果您担心存在多个指向相同数据的指针,或者特定内存块没有明确的所有者,那么智能指针是一个很好的解决方案。 Boost 的 shared_ptr 可能是一个不错的选择,并且 shared_ptr 将成为 C++0x 的一部分。许多人建议您应该始终使用共享指针,但这会涉及一些开销,并且它是否最适合您的特定应用程序将完全取决于您的应用程序。

最终,您需要了解各种容器类型的优点和缺点,并确定最适合您所做的事情的容器。如何处理指针管理也是如此。您需要以明确谁拥有特定内存块的方式编写程序,并确保所有者在适当的时候释放它。共享指针只是解决方案之一(尽管是一个很好的解决方案)。最佳解决方案是什么取决于您的程序的具体情况。

There's nothing wrong with storing pointers in a standard container - be it a vector, set, map, or whatever. You just have to be aware of who owns that memory and make sure that it's released appropriately. When choosing a container, choose the container that makes the most sense for your needs. vector is great for random access and appending but not so great for inserting elsewhere in the container. list deals with insertions extremely well, but it doesn't have random access. Sets ensure that there are no duplicates in the container and it's sorted (though the sorting isn't very useful if the set holds pointers and you don't give a comparator function) whereas a map is a set of key-value pairs, so sorting and access is done by key. Etc. Etc. Every container has its pros and cons and which is best for a particular situation depends entirely on that situation.

As for pointers, again, having pointers in containers is fine. The issue that you need to worry about is who owns the memory and therefore must worry about freeing it. If there is a clear object that owns what a particular pointer points to, then it should probably be that object which frees it. If it's essentially the container which owns the memory, then you need to make sure that you delete all of the pointers in the container before the container is destroyed.

If you are concerned with there being multiple pointers to the same data floating around or there is no clear owner for a particular chunk of memory, then smart pointers are a good solution. Boost's shared_ptr would probably be a good one to use, and shared_ptr will be part of C++0x. Many would suggest that you should always use shared pointers, but there is some overhead involved and whether it's best for your particular application will depend entirely on your application.

Ultimately, you need to be aware of the strengths and weaknesses of the various container types and determine what the best container is for whatever you're doing. The same goes for how to deal with pointer management. You need to write your program in a way that it's clear who owns a particular chunk of memory and make sure that that owner frees it when appropriate. Shared pointers are just one solution for that (albeit an excellent one). What the best solution is depends on the particulars of your program.

情深缘浅 2024-10-06 10:47:32

首先为什么会有重复项?如果类 A 是负责创建实例的唯一实体,并且它私有地持有容器,这意味着其他人无法改变它,那么在我看来,应该没有理由出现重复。好吧,如果有的话,在将指针添加到向量之前是否可以通过一些检查来补救?

我不知道为什么将指针存储在什么样的容器中会很重要。容器并不真正操纵它们的数据,它们只是以不同的方式提供对数据的访问。所以,这取决于你:)

Why would there be any duplicates in the first place? If class A is the sole entity responsible for creating the instances, and it holds the container privately, meaning there's no way for others to mutate it, it seems to me that there should be no cause for duplicates. Well, if there is, won't that be remediable by some checking prior to adding the pointer to the vector?

I don't know why it would matter if you store a pointer in what kind of container. Containers don't really manipulate their data, they only provide access to them in different ways. So, it's up to you :)

萧瑟寒风 2024-10-06 10:47:32

如果需要在 stl 容器中存储指针,请使用shared_ptr。

现在看来,这一套听起来完全错误。你打算用这些做什么?
如果需要添加和删除,则列出。
如果您需要迭代范围或全部,则使用向量。
如果您需要访问特定的,知道密钥,然后映射。
也看看其他人。一种尺寸并不适合所有情况。

If you need to store pointers in stl containers, use shared_ptr.

Now, the set sounds completely wrong. What are you going to do with those?
If you need to add and remove, then list.
If you need to iterate over range, or all, then vector.
If you need to access specific, knowing a key, then map.
Take a look at others as well. One size doesn't fit all.

带上头具痛哭 2024-10-06 10:47:32

我的答案是,你做出的任何决定都必须考虑到你的目标。如果您需要集合强制执行的“不允许重复”规则,请使用集合。如果没有,那么您可能需要使用向量或任何容器都可以解决问题。

至于 smart_pointers 是的,它们确实非常有用。应该使用它们吗?我不知道,我再次不知道你的最终目标是什么,或者你试图与他们解决的问题。

基本上都是这样的。如果我说“我想用锤子。你觉得怎么样?”你可能会说“那有什么用,我知道锤子非常适合钉钉子和木头场景,但它们也可以用作伤害人的工具,或者用作书架。看,等一下,这是什么?又来一次?”问题是我还没有真正说出为什么我要用锤子。我还没有说我想达到什么目标。

因此,如果您有某种总体目标,那么为什么不让我们知道,那么您是否使用正确的工具来完成工作就会很明显,我们可以为您提供更多帮助。

My answer is that any decisions that you make have be be with your goal in mind. If you need a 'no duplicates allowed' rule that a set enforces, then use a set. If not then you might want to use a vector or any container might do the trick.

As for smart_pointers yes, they are really really useful. Should they be used? I don't know, once again I don't know what your end goal is or the problem that you are trying to solve with them.

Basically it comes down this. If I said "I want to use a hammer. What do you think of that?" you would probably say "Well what for, I know that hammers are pretty good for nails and wood scenarios but they could also be used as a tool to hurt people or maybe as a book stand. Look, just wait a second, what is this for again?" The problem is that I have not, really said why I want to use a hammer. I have not said what goal I am trying to achieve.

So if you have sort of an overall goal then why not let us know, then it will be obvious if you are using the right tools for the job and we can help you more.

可可 2024-10-06 10:47:32

在我看来,坚持使用矢量,除非你有真正的理由不这样做。与向量相比,集合具有一些运行时开销以及相当大的语义开销。

In my opinion, stick with vector unless you have a real reason not to. Sets come with some runtime overhead as well as quite large semantic overhead compared to a vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文