Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量<;类别*>;和向量<;类别>;?_C++_Templates_Vector_Std - Fatal编程技术网

C++ 向量<;类别*>;和向量<;类别>;?

C++ 向量<;类别*>;和向量<;类别>;?,c++,templates,vector,std,C++,Templates,Vector,Std,std::vector和std::vector之间有什么区别? 此外,在提高效率和避免错误方面,哪一个更适合使用?vector存储指向对象的指针列表,对象本身必须单独分配,因为它们是指针,您可以使用多态行为,例如: vector<Foo*> list; list.push_back( new Bar() ); // class Bar : Foo list.push_back( somePointerToFoo ); list[0]->someVirtualMethod();

std::vector
std::vector
之间有什么区别? 此外,在提高效率和避免错误方面,哪一个更适合使用?

vector
存储指向对象的指针列表,对象本身必须单独分配,因为它们是指针,您可以使用多态行为,例如:

vector<Foo*> list;
list.push_back( new Bar() ); // class Bar : Foo
list.push_back( somePointerToFoo );
list[0]->someVirtualMethod();
…但如果向量存储的指针是从聚合的多个源收集的,则需要确定列表是否“拥有”指针,并根据需要删除对象

vector
将实际类值(其字段)内联存储在向量本身中,这意味着操作通常涉及复制所有值,这可能是预期行为,也可能不是预期行为。您始终可以创建指向这些元素的指针,但随后您将进入危险区域:

vector<Foo> list;
list.push_back( Foo() ); // construct an instance of Foo, which might be copied up to 3 times in this single operation, depending on how smart the compiler+library is
list.push_back( *somePointerToFoo ); // this will dereference and copy this instance of Foo
list[0].someVirtualMethod(); // will not be a vtable call

list.push_back( Bar() ); // forbidden, Bar does not fit into Foo
向量列表;
list.push_back(Foo());//构造一个Foo实例,根据编译器+库的智能程度,在这个操作中最多可以复制3次
列表。向后推(*somePointerToFoo);//这将取消引用并复制此Foo实例
列表[0]。someVirtualMethod();//不会是一个vtable呼叫
list.push_back(Bar());//禁止使用,酒吧不适合使用Foo
嗯,
std::vector
是一个动态调整大小的
T
s数组

如果
T
Class*
,它存储指向
Class
的指针,如果是
Class
,则存储类型为
Class
的对象的指针

一般来说,您根本不应该使用原始指针,至少不应该用于拥有指针。看看智能指针

如果您确实想要存储多态类
class
或派生类的对象,则必须使用指针或某种多态容器

关于效率,这取决于移动对象的难易程度、对象的大小以及移动的频率。
换句话说,魔鬼在于细节,而你没有提供这些细节

vector
vector

vector
存储对象,而
vector
存储指向对象的指针。当你将一个对象推入第一类向量时,它会被复制;第二个向量存储指针,但对象保持不变

此外,在效率和避免错误方面,哪一个更好

让我们从避免错误开始:如果可能,请使用
vector
,因为该类分配的任何资源都将由vector自动管理。如果
vector
保留原始对象,则您将负责在完成后调用
delete

这可能不可能在所有情况下都实现,因此解决方案不是通用的


就效率而言,
vector
允许您避免复制,因此理论上它可能更有效。即使在这种情况下,您也应该更喜欢智能指针,例如
std::shared_ptr
std::unique_ptr
,而不是内置指针,因为智能指针将帮助您实现资源管理的自动化。

只有在它实际拥有指针的情况下,才能将Preference
std::vector
复制到
std::vector
@JonathanPotter……我建议添加一条关于解除分配对象乐趣的注释。还有一个问题。这是删除指向对象的指针的正确方法吗?对于(size_t i=0;iclear()
也是一个好主意,因为它可以帮助您避免错误地访问已删除的指针。如果原始指针不能控制所包含对象的生存期,则可以使用原始指针。指针可能更有效,但由于现在无法预测的对象在内存中的位置,如果对象很小,并且要用于高速计算,那么它可能会变成死机。
vector<Foo> list;
list.push_back( Foo() ); // construct an instance of Foo, which might be copied up to 3 times in this single operation, depending on how smart the compiler+library is
list.push_back( *somePointerToFoo ); // this will dereference and copy this instance of Foo
list[0].someVirtualMethod(); // will not be a vtable call

list.push_back( Bar() ); // forbidden, Bar does not fit into Foo