Possible Duplicate:
C++ STL: Which method of iteration over a STL container is better?可能重复:C ++ STL:哪种迭代方法比STL容器更好?
In my current project, I have an STL Deque of pointers. I also have a method that is meant to delete all of the objects that those pointers are pointing to. I've come up with two different ways to achieve this, but I can't decide which is the preferred method.
在我目前的项目中,我有一个STL Deque of pointers。我还有一个方法,用于删除那些指针指向的所有对象。我想出了两种不同的方法来实现这一点,但我无法确定哪种方法是首选方法。
Method 1.
for (deque<MyType*>::const_iterator it = myDeque.begin(); it != myDeque.end(); ++it)
{
delete *it;
}
myDeque.clear();
Method 2.
for (int i = 0; i < myDeque.size(); ++i)
{
delete myDeque[i];
}
myDeque.clear();
Both of these methods should work, but which one would be preferred? Method 1 makes use of the STL, but Method 2 is much simpler. Other than code cleanliness, is there any reason why one method should be used over the other? Is there any advantage to using an iterator in this scenario despite the little bit of overhead that goes into creating it?
这两种方法都应该有效,但哪种方法更受欢迎?方法1使用STL,但方法2更简单。除了代码清洁度之外,还有什么理由可以使用一种方法而不是另一种方法?在这种情况下使用迭代器是否有任何优势,尽管创建它的开销很小?
Note: This question applies to other STL sequence containers, not just Deques.
注意:此问题适用于其他STL序列容器,而不仅仅是Deques。
3 个解决方案
#1
2
Unless you really need to use a collection of pointers, just use a collection of objects and be done with it.
除非你真的需要使用指针集合,否则只需使用一组对象并完成它。
If you truly need a collection of pointers, then you probably want to use something like a Boost pointer container to automate deleting the pointee items.
如果你真的需要一个指针集合,那么你可能想要使用类似Boost指针容器的东西来自动删除指针项。
#2
1
I would say 1 is more efficient, as incrementing is more efficient than random access. Random access requires retrieving the necessary constant and do a full addition while incrementing is often implemented very efficiently in the hardware.
我认为1更有效,因为递增比随机访问更有效。随机访问需要检索必要的常量并进行完全添加,而增量通常在硬件中非常有效地实现。
But of course, initializing an iterator costs something, so there is a threshold on number of elements where iterator becomes more efficient, but I expect that number to be quite low.
但是,当然,初始化迭代器需要花费一些成本,因此迭代器变得更有效的元素数量存在阈值,但我希望这个数字非常低。
#3
1
I don't know if there is any difference between those two methods, but I would use this:
我不知道这两种方法之间是否有任何区别,但我会用这个:
std::vector<boost::shared_ptr<MyType*> > vec
#1
2
Unless you really need to use a collection of pointers, just use a collection of objects and be done with it.
除非你真的需要使用指针集合,否则只需使用一组对象并完成它。
If you truly need a collection of pointers, then you probably want to use something like a Boost pointer container to automate deleting the pointee items.
如果你真的需要一个指针集合,那么你可能想要使用类似Boost指针容器的东西来自动删除指针项。
#2
1
I would say 1 is more efficient, as incrementing is more efficient than random access. Random access requires retrieving the necessary constant and do a full addition while incrementing is often implemented very efficiently in the hardware.
我认为1更有效,因为递增比随机访问更有效。随机访问需要检索必要的常量并进行完全添加,而增量通常在硬件中非常有效地实现。
But of course, initializing an iterator costs something, so there is a threshold on number of elements where iterator becomes more efficient, but I expect that number to be quite low.
但是,当然,初始化迭代器需要花费一些成本,因此迭代器变得更有效的元素数量存在阈值,但我希望这个数字非常低。
#3
1
I don't know if there is any difference between those two methods, but I would use this:
我不知道这两种方法之间是否有任何区别,但我会用这个:
std::vector<boost::shared_ptr<MyType*> > vec