与引用一起使用的STL容器 [英] STL-container used with references

查看:71
本文介绍了与引用一起使用的STL容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


通常我会简单地执行以下操作:


std :: vector< Element> vec;

void somefunc(){

元素e;

vec.push_back(e);

}

现在
元素e在向量中。只要不需要多态的行为就可以了。那就是
std :: vector< Element&> vec;

void somefunc(){

Derived_from_Element e;

vec.push_back(e); //糟糕的主意,......在功能望远镜离开后,e就消失了

}


我想避免的(如果可能的话)是两件事:

对象的动态分配,以及从basetype派生的每个

类型的额外容器来存储元素。


i希望,我明确了我要做的事情......所以有一个解决方案(除了

上面提到的2个?)


thx,问候,

sev

hi!

normally i would simply do the following:

std::vector<Element> vec;
void somefunc() {
Element e;
vec.push_back(e);
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<Element &> vec;
void somefunc() {
Derived_from_Element e;
vec.push_back(e); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i''m trying to do... so is there a solution (except
for the 2 mentioned above?)

thx, regards,
sev

推荐答案

2004年4月6日星期二15:04:53 +0200,Severin Ecker ; < se **** @ gmx.at>

写道:
On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <se****@gmx.at>
wrote:
嗨!

通常我只会这样做以下:

std :: vector< Element> vec;
void somefunc(){
元素e;
vec.push_back(e);
}
现在元素e在向量中。只要不需要多态的行为,那就好了。

std :: vector< Element&> VEC;


你不能容纳容器中的引用 - 引用只是别名,

不是真正的对象。

void somefunc( ){
Derived_from_Element e;
vec.push_back(e); //糟糕的主意,...在功能望远镜离开后,e已经消失
我希望避免(如果可能的话)是两件事:
动态分配对象,并且从基本类型派生的每种类型都有一个额外的容器来存储元素。

我希望,我明确表示我正在尝试做什么......所以有解决方案吗? (除了上面提到的2个?)
hi!

normally i would simply do the following:

std::vector<Element> vec;
void somefunc() {
Element e;
vec.push_back(e);
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<Element &> vec;
You can''t hold references in containers - references are just aliases,
not real objects.
void somefunc() {
Derived_from_Element e;
vec.push_back(e); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i''m trying to do... so is there a solution (except
for the 2 mentioned above?)




显然很难避免动态分配对象,因为

所有派生类型都可以有不同的大小和对齐方式

要求。有这样做的技巧(只要提前知道所有

派生类),但是在这种情况下它们是不成熟的,但我确信这是优化的。你最好的选择是使用一个

的智能指针容器:


std :: vector< shared_ptr< Element> > vec;

vec.push_back(shared_ptr< Element>(new Derived_from_Element));


参见 www.boost.org for shared_ptr。


Tom

-

C ++常见问题解答: http://www.parashift。 com / c ++ - faq-lite /

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html



It is obviously hard to avoid dynamic allocation of the objects, since
all of your derived types can have different sizes and alignment
requirements. There are techniques for doing it (as long as all
derived classes are known in advance), but they would be premature
optimization in this case I am sure. Your best bet is to use a
container of smart pointers:

std::vector<shared_ptr<Element> > vec;
vec.push_back(shared_ptr<Element>(new Derived_from_Element));

See www.boost.org for shared_ptr.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


On周二,2004年4月6日15:04:53 +0200,Severin Ecker < se **** @ gmx.at>

写道:
On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <se****@gmx.at>
wrote:
嗨!

通常我只会这样做以下:

std :: vector< Element> vec;
void somefunc(){
元素e;
vec.push_back(e);
}
现在元素e在向量中。只要不需要多态的行为,那就好了。

std :: vector< Element&> VEC;


你不能容纳容器中的引用 - 引用只是别名,

不是真正的对象。

void somefunc( ){
Derived_from_Element e;
vec.push_back(e); //糟糕的主意,...在功能望远镜离开后,e已经消失
我希望避免(如果可能的话)是两件事:
动态分配对象,并且从基本类型派生的每种类型都有一个额外的容器来存储元素。

我希望,我明确表示我正在尝试做什么......所以有解决方案吗? (除了上面提到的2个?)
hi!

normally i would simply do the following:

std::vector<Element> vec;
void somefunc() {
Element e;
vec.push_back(e);
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<Element &> vec;
You can''t hold references in containers - references are just aliases,
not real objects.
void somefunc() {
Derived_from_Element e;
vec.push_back(e); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i''m trying to do... so is there a solution (except
for the 2 mentioned above?)




显然很难避免动态分配对象,因为

所有派生类型都可以有不同的大小和对齐方式

要求。有这样做的技巧(只要提前知道所有

派生类),但是在这种情况下它们是不成熟的,但我确信这是优化的。你最好的选择是使用一个

的智能指针容器:


std :: vector< shared_ptr< Element> > vec;

vec.push_back(shared_ptr< Element>(new Derived_from_Element));


参见 www.boost.org for shared_ptr。


Tom

-

C ++常见问题解答: http://www.parashift。 com / c ++ - faq-lite /

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html



It is obviously hard to avoid dynamic allocation of the objects, since
all of your derived types can have different sizes and alignment
requirements. There are techniques for doing it (as long as all
derived classes are known in advance), but they would be premature
optimization in this case I am sure. Your best bet is to use a
container of smart pointers:

std::vector<shared_ptr<Element> > vec;
vec.push_back(shared_ptr<Element>(new Derived_from_Element));

See www.boost.org for shared_ptr.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html




" tom_usenet" <到******** @ hotmail.com>在消息中写道

"tom_usenet" <to********@hotmail.com> wrote in message
...最好的办法是使用一个智能指针容器:

std :: vector< shared_ptr< Element> > vec;
vec.push_back(shared_ptr< Element>(new Derived_from_Element));

参见 www.boost.org for shared_ptr。
...Your best bet is to use a
container of smart pointers:

std::vector<shared_ptr<Element> > vec;
vec.push_back(shared_ptr<Element>(new Derived_from_Element));

See www.boost.org for shared_ptr.




好​​奇......如果你没有(或想要)怎么办?促进?是否有类似于shared_ptr的STL

解决方案?


-Howard



Just curious...What if you don''t have (or want) boost? Is there an STL
solution similar to shared_ptr?

-Howard


这篇关于与引用一起使用的STL容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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