std :: vector< mytype>分配策略 [英] std::vector <mytype> allocation strategy

查看:70
本文介绍了std :: vector< mytype>分配策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




使用std :: vector< type>是否更好?指向存储的类型是什么?b $ b存储类型的实际存储?换句话说,是

std :: vector< mytype>或std :: vector< mytype *>好吗?我知道

有具体情况需要说明,但在一般情况下,

您怎么看?


谢谢,

Lynn McGuire

解决方案

Lynn写道:

使用std更好吗:: vector< type>指向存储类型的指针或类型的实际存储?换句话说,是
std :: vector< mytype>或std :: vector< mytype *>好吗?我知道
有特定情况需要说明,但在一般情况下,你觉得怎么样?




可能没有除了取决于之外的其他答案。没有一般

案例为此。


V


" Lynn" < NO **** @ NOSPAM.com>在消息中写道

news:cp ******** @ library1.airnews.net ...

使用std是否更好: :vector< type>指向存储类型的指针或类型的实际存储?换句话说,是
std :: vector< mytype>或std :: vector< mytype *>更好?




如果你不得不问,那么答案是最好不要使用向量

指针。我对这个答案的理由是,如果你使用指针,那么你需要负责管理指针指向的对象。

如果你知道你需要知道的一切为了履行这个

的责任,你可能不需要在第一个

的地方提问。

>>使用std :: vector< type>是否更好?指向

存储的类型或类型的实际存储?换句话说,是
std :: vector< mytype>或std :: vector< mytype *>更好?



如果你不得不问,那么答案是最好不要使用指针向量。我对这个答案的理由是,如果你使用指针,你就要负责管理指针指向的对象。如果您知道所需要的一切知识以便履行这一责任,您可能不需要首先提出问题。



$ b $是的,这就是我的想法。当使用内置于向量中的自动内存

mangement时,可以获得广泛的经验和广度

的良好工作和调试代码。为什么复制这个功能

,除非你的代码已经预期在向量中有空指针

(我正在升级的当前代码没有)。


谢谢,

Lynn


Hi,

Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ? I know that
there are specific cases where it is dictated but in the general case,
what do you think ?

Thanks,
Lynn McGuire

解决方案

Lynn wrote:

Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ? I know that
there are specific cases where it is dictated but in the general case,
what do you think ?



There can be no other answer than "it depends". There is no "general
case" for this.

V


"Lynn" <NO****@NOSPAM.com> wrote in message
news:cp********@library1.airnews.net...

Is it better to use std::vector <type> with a pointer to the types being
stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ?



If you have to ask, then the answer is that it is better not to use vectors
of pointers. My rationale for this answer is that if you use pointers, you
take on responsibility for managing the objects to which the pointers point.
If you knew everything you need to know in order to discharge this
responsibility, you probably wouldn''t need to ask the question in the first
place.


>> Is it better to use std::vector <type> with a pointer to the types being

stored or the actual storage for the types ? In other words, is
std::vector <mytype> or std::vector <mytype *> better ?



If you have to ask, then the answer is that it is better not to use vectors of pointers. My rationale for this answer is that if
you use pointers, you take on responsibility for managing the objects to which the pointers point. If you knew everything you need
to know in order to discharge this responsibility, you probably wouldn''t need to ask the question in the first place.



Yup, that is what I am thinking. When one uses the automatic memory
mangement built into vector, one gains a wide experience and breadth
of well working and debugged code. Why duplicate this functionality
unless your code already expects to have null pointers in the vector
(which my current code that I am upgrading does not).

Thanks,
Lynn


这篇关于std :: vector&lt; mytype&gt;分配策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆