问题描述
我有std :: vector< Base * base;
我想做类似的事情:
std :: for_each(bases .begin(),bases.end(),operator delete);
是否可以不编写适配器?有没有更好的办法?
有现有适配器吗?
谢谢,
Daniel。
-
Daniel Pitts的'科技博客:< http://virtualinfinity.net/wordpress/>
保持对动态对象的简单指针是太麻烦了。
智能指针是最好的方法。 boost :: shared_ptr是合适的:
#include< boost / shared_prt.hpp>
/ * ... * /
typedef boost :: shared_ptr< BaseBasePtr;
std :: vector< BasePtrbases;
这个和其他问题神奇地消失了......
Ali
出了什么问题:
for(std :: vector< Base *> :: iterator i = bases.begin(); i!=
bases.end(); ++ i)
delete * i;
它只有两条线;而且很明显发生了什么。我不认为
你能打败它。如果你经常这样做,你可以写一个实用函数,如:
模板< class Tvoid delete_all(T& cont){
typedef typename T :: iterator iter_t;
for(iter_t i(cont.begin()); i!= cont.end(); ++ i)
删除* i;
cont.clear();
}
示例:
struct A {...};
void f(){
vector< A * x;
list< A * y;
set< A * z;
x.push_back(new A);
x.push_back(新A);
y.push_back(新A);
y.push_back(新A);
z.insert(新A);
z.insert(新A);
delete_all(x);
delete_all(y);
delete_all(z);
}
Jason
看起来不是这样......你最好写自己的my_delete:
模板< class T>
struct my_delete:std :: unary_function< T,void>
{
void operator()(T * t)
{
删除t;
}
};
这一个:
std :: for_each(bases.begin(),bases.end(),std :: ptr_fun(operator delete));
编译,但不起作用(dtor未被调用)...
-
William
Underdogging:
在给定的情况下,几乎总是与弱者站在一起的倾向。这种特性的消费者表达是购买不太成功的悲伤,悲伤,或者是失败的产品:我知道这些维也纳弗兰克人棒棒糖是心脏衰竭,但他们太伤心了
查看所有其他雅皮士食品我只需要
就可以买到它们。
- Douglas Coupland,X世代:加速的故事
文化< br />
I have std::vector<Base *bases;
I''d like to do something like:
std::for_each(bases.begin(), bases.end(), operator delete);
Is it possible without writing an adapter? Is there a better way? Is
there an existing adapter?
Thanks,
Daniel.
--
Daniel Pitts'' Tech Blog: <http://virtualinfinity.net/wordpress/>
Keeping plain pointers to dynamic objects is way too much trouble.
Smart pointers is the best way. boost::shared_ptr is suitable:
#include <boost/shared_prt.hpp>
/* ... */
typedef boost::shared_ptr<BaseBasePtr;
std::vector<BasePtrbases;
This and other problems magically vanish...
Ali
What''s wrong with:
for (std::vector<Base *>::iterator i = bases.begin(); i !=
bases.end(); ++ i)
delete *i;
It''s only two lines; and it''s clear what is happening. I don''t think
you''ll be able to beat that. If it''s something you do frequently you
could write a utility function like:
template <class Tvoid delete_all (T &cont) {
typedef typename T::iterator iter_t;
for (iter_t i(cont.begin()); i != cont.end(); ++ i)
delete *i;
cont.clear();
}
Example:
struct A { ... };
void f () {
vector<A *x;
list<A *y;
set<A *z;
x.push_back(new A);
x.push_back(new A);
y.push_back(new A);
y.push_back(new A);
z.insert(new A);
z.insert(new A);
delete_all(x);
delete_all(y);
delete_all(z);
}
Jason
Doesn''t seem so... You''d better write your own my_delete:
template <class T>
struct my_delete : std::unary_function<T, void>
{
void operator()(T* t)
{
delete t;
}
};
This one:
std::for_each(bases.begin(), bases.end(), std::ptr_fun(operator delete));
compiles, but doesn''t work(dtor is not called)...
--
William
http://williamxu.net9.org
Underdogging:
The tendency to almost invariably side with the underdog in a
given situation. The consumer expression of this trait is the
purchasing of less successful, "sad," or failing products: "I know
these Vienna franks are heart failure on a stick, but they were so sad
looking up against all the other yuppie food items that I just had to
buy them."
-- Douglas Coupland, "Generation X: Tales for an Accelerated
Culture"
这篇关于“删除”的最佳方式std :: vector中的所有对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!