我应该在容器内使用指针吗? [英] Should I use pointer inside container?

查看:84
本文介绍了我应该在容器内使用指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨@全部!


再次提出一个小问题,因为我对使用的东西感到羞耻...


什么是更好的/更聪明?


私人:

向量< MyClass_t * itsVector;


或者......


私人:

向量< MyClass_t * * itsVector;


如何运行这样的stl容器?它是使用堆还是堆栈?


谢谢


Goran

Hi @ all!

Again one small question due to my shakiness of what to use...

What is better / smarter?

private:
vector<MyClass_t* itsVector;

OR...

private:
vector<MyClass_t * * itsVector;

How works such a stl container? Does it use heap or stack?

Thanks

Goran

推荐答案

Goran写道:
Goran wrote:

嗨@ all!


再一个小问题归结于我使用什么的晃动...


什么是更好/更聪明?
Hi @ all!

Again one small question due to my shakiness of what to use...

What is better / smarter?



STL包含要求元素可分配

,因为它复制了push_xxxx和插入的每个元素。


所以,如果你的元素不可分配,那么当你的元素可以赋值时,除了

之外别无其他方法可以将指针用作元素


,那么你也有选择,主要原因是你的元素是否是大对象。

如果是则使用指针避免复制

STL contains requires the element to be assignable
as it copy each element you push_xxxx and insert.

So, if your element is not assignable, then you have no other way than
to use pointer as element

when your element is assignable, then you have too choose, the major
principle is that whether your element is big object.
if is then use pointer to avoid copy


>

私人:

向量< MyClass_t * itsVector;


OR .. 。


私人:

向量< MyClass_t * * itsVector;


如何运行这样的stl容器?它使用堆还是堆栈?
>
private:
vector<MyClass_t* itsVector;

OR...

private:
vector<MyClass_t * * itsVector;

How works such a stl container? Does it use heap or stack?



你必须参考C ++教科书或查看一些实现代码。

-

谢谢

Barry

you have to refer to a C++ text book or check out some implementation code.
--
Thanks
Barry


2007-09-08 16:04,Goran写道:
On 2007-09-08 16:04, Goran wrote:

嗨@全部!


再次提出一个小问题,因为我对使用的东西感到羞耻...


什么是更好的/更聪明?


私人:

向量< MyClass_t * itsVector;


OR ...


私人:

vector< MyClass_t * * itsVector;
Hi @ all!

Again one small question due to my shakiness of what to use...

What is better / smarter?

private:
vector<MyClass_t* itsVector;

OR...

private:
vector<MyClass_t * * itsVector;



以上都不是,使用


std :: vector< MyClassitsVector;





std :: vector< MyClass * itsVector;


你应该使用哪一个取决于你将如何使用元素,如果

你没有特殊需要,第一个通常更可取,因为

它不需要新的和手动删除元素。

可能有一些合法的情况,你可能需要一个

指向一个向量的指针,但我会说它通常是一个坏的迹象

设计。

None of the above, use either

std::vector<MyClassitsVector;

or

std::vector<MyClass*itsVector;

Which one you should use depends on how you will use the elements, if
you have not special needs the first one is generally preferable since
it removes the need to new and delete the elements manually.

There might be a few legitimate situations where you might need a
pointer to a vector, but I would say that it is most often a sign of bad
design.


如何运行这样的stl容器?它使用堆还是堆栈?
How works such a stl container? Does it use heap or stack?



向量中包含的元素存储在堆上,注意

认为对于某些容器,其中有std :: vector,当您对容器执行某些操作时,可能会重新找到元素



因此获取对象的地址可能是危险的。

事实上,我会说,当你发现自己使用指针时,如果你确实需要它,请自己询问

。在大多数情况下,使用参考分配堆栈或

也一样好。


-

Erik Wikstr?m

The elements contained in the vector are stored on the heap, notice
thought that for some containers, std::vector among them, the elements
might be re-located when you perform some operations on the containers,
so taking the address of an object can be hazardous.

In fact, I would say that when you find yourself using a pointer ask
yourself if you really need it. In most cases allocating on the stack or
using a reference is just as good.

--
Erik Wikstr?m


9月8日上午10:04,Goran< postmasch ... @ gmail.comwrote:
On Sep 8, 10:04 am, Goran <postmasch...@gmail.comwrote:

嗨@全部!


再次提出一个小问题,因为我对使用的东西感到羞耻...


什么是更好/更聪明?


私人:

向量< MyClass_t * itsVector;


或者......


私人:

向量< MyClass_t * * itsVector;


如何运行这样的stl容器?它是使用堆还是堆栈?


谢谢


Goran
Hi @ all!

Again one small question due to my shakiness of what to use...

What is better / smarter?

private:
vector<MyClass_t* itsVector;

OR...

private:
vector<MyClass_t * * itsVector;

How works such a stl container? Does it use heap or stack?

Thanks

Goran



快速阅读本文但你可能想参考以前的帖子

我的:
http://groups.google.com/group/comp .... 9d782f6d6dae52


google ptr_vector

quickly read this but you might want to refer to a previous post of
mine:
http://groups.google.com/group/comp....9d782f6d6dae52

also google ptr_vector


这篇关于我应该在容器内使用指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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