向量 <Class*> 和向量 <Class> 有什么区别?
What is the difference between vector <Class*> and vector <Class>?
std::vector<Class*>
和 std::vector<Class>
有什么区别?
而且,在效率和避免错误方面,使用哪个更好?
vector<Class*>
存储指向对象的指针列表,对象本身必须单独分配,因为它们是指针,您可以使用多态行为,如下所示:
vector<Foo*> list;
list.push_back( new Bar() ); // class Bar : Foo
list.push_back( somePointerToFoo );
list[0]->someVirtualMethod();
请注意,使用这种方法时,您必须手动 delete
创建对象。它可能就这么简单:
for(Foo* f : list) delete f;
...但是如果您的向量存储的指针是从您正在聚合的多个来源收集的,那么您需要确定是否列出 "owns" 指针并酌情删除对象。
vector<Class>
将实际的 class 值(其字段)内联存储在向量本身中,这意味着操作通常涉及复制所有值,这可能是也可能不是预期的行为。您始终可以创建指向这些元素的指针,但这样您就进入了危险的领域:
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
好吧,std::vector<T>
是一个可动态调整大小的 T
数组。
如果T
是Class*
,它存储指向Class
的指针,如果它是Class
,那么Class
.[=19类型的对象=]
一般来说,您根本不应该使用原始指针,至少不应该拥有指针。看看智能指针。
如果你真的想存储多态的对象 class Class
或派生的,你必须使用指针,或某种多态容器。
关于效率,这取决于Class
-对象移动的难易程度、对象的大小以及移动的频率。
换句话说,细节决定成败,而你却没有提供这些细节。
What is the difference between vector<Class*>
and vector<Class>
vector<Class>
存储对象,而 vector<Class*>
存储指向对象的指针。当你将一个对象推入第一种向量时,它会被 copied;第二个向量存储指针,但对象保留在原位。
And also, which one is better to work with when it comes to efficiency and to avoid errors?
让我们从避免错误开始:如果可能的话,请使用 vector<Class>
,因为 class 分配的任何资源都将由 vector 自动管理。如果 vector<*Class>
持有原始对象,您将负责在完成后调用 delete
。
由于object slicing,这可能并非在所有情况下都可行,因此解决方案并不通用。
就效率而言,vector<*Class>
可以避免复制,因此理论上 可以 更高效。即使在那种情况下,您也应该更喜欢智能指针,例如 std::shared_ptr<T>
或 std::unique_ptr<T>
,而不是内置指针,因为智能指针将帮助您自动化资源管理。
std::vector<Class*>
和 std::vector<Class>
有什么区别?
而且,在效率和避免错误方面,使用哪个更好?
vector<Class*>
存储指向对象的指针列表,对象本身必须单独分配,因为它们是指针,您可以使用多态行为,如下所示:
vector<Foo*> list;
list.push_back( new Bar() ); // class Bar : Foo
list.push_back( somePointerToFoo );
list[0]->someVirtualMethod();
请注意,使用这种方法时,您必须手动 delete
创建对象。它可能就这么简单:
for(Foo* f : list) delete f;
...但是如果您的向量存储的指针是从您正在聚合的多个来源收集的,那么您需要确定是否列出 "owns" 指针并酌情删除对象。
vector<Class>
将实际的 class 值(其字段)内联存储在向量本身中,这意味着操作通常涉及复制所有值,这可能是也可能不是预期的行为。您始终可以创建指向这些元素的指针,但这样您就进入了危险的领域:
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
好吧,std::vector<T>
是一个可动态调整大小的 T
数组。
如果T
是Class*
,它存储指向Class
的指针,如果它是Class
,那么Class
.[=19类型的对象=]
一般来说,您根本不应该使用原始指针,至少不应该拥有指针。看看智能指针。
如果你真的想存储多态的对象 class Class
或派生的,你必须使用指针,或某种多态容器。
关于效率,这取决于Class
-对象移动的难易程度、对象的大小以及移动的频率。
换句话说,细节决定成败,而你却没有提供这些细节。
What is the difference between
vector<Class*>
andvector<Class>
vector<Class>
存储对象,而 vector<Class*>
存储指向对象的指针。当你将一个对象推入第一种向量时,它会被 copied;第二个向量存储指针,但对象保留在原位。
And also, which one is better to work with when it comes to efficiency and to avoid errors?
让我们从避免错误开始:如果可能的话,请使用 vector<Class>
,因为 class 分配的任何资源都将由 vector 自动管理。如果 vector<*Class>
持有原始对象,您将负责在完成后调用 delete
。
由于object slicing,这可能并非在所有情况下都可行,因此解决方案并不通用。
就效率而言,vector<*Class>
可以避免复制,因此理论上 可以 更高效。即使在那种情况下,您也应该更喜欢智能指针,例如 std::shared_ptr<T>
或 std::unique_ptr<T>
,而不是内置指针,因为智能指针将帮助您自动化资源管理。