peakzhang 发表于 2007-12-21 22:10:18

如何使用标准库存储自定义类,存储自定义类指针

我们想使用标准库来对自己定义的类来操作,比如list,map ,vector,我定义了新的类
myclass,我想用
typedef std::list<myclass> ;
typedef std::vector<myclass> ;
typedef std::map <char*,myclass> T_applCfg_Map;
这样使用好?还是用类指针存储好?
typedef std::list<myclass*> ;
typedef std::vector<myclass*> ;
typedef std::map <char*,myclass*> T_applCfg_Map;
请高手提供实际经验中的使用。谢谢。

peakzhang 发表于 2007-12-21 22:10:39

我的经验:
其实没有固定的模式,有的时候,用对象合适,有的时候,用对象的指针合适。
在你存储的信息内容,不需要怎么更改的时候,用对象舒服,因为生存期由STL控制了。如果需要频繁更改容器中的数据,用指针更好,避免对象复制的消耗,性能更优秀。
但各中细节,请自己写程序慢慢体会吧。

peakzhang 发表于 2007-12-21 22:10:47

考虑到效率问题的话,就要用对象指针!

peakzhang 发表于 2007-12-21 22:11:18

如果不存在成本非常高的constructor & destructor的话,尽量用对象的方式.

否则参考使用smart_ptr作为wrapper放入container中.

peakzhang 发表于 2007-12-21 22:11:25

否则参考使用smart_ptr作为wrapper放入container中.
I can smell core dump here. It's more complex than you thought to
put a smart pointer into a container. I never put std::auto_ptr into
stl::container, jus my .02.

nanker 发表于 2010-11-13 22:46:45

果然是超级版主,自问自答啊。
页: [1]
查看完整版本: 如何使用标准库存储自定义类,存储自定义类指针