STL问题 [英] STL question
问题描述
如果我的容器
包含指针,有没有办法使用STL算法for_each?例如,
A级
{
int a;
虚拟空白打印(无效){cout<< a<<结束;}
};
B级:公共A
{
int b;
virtual void print(void){cout<< a<< "," << b<< endl;}
}
我想要一个列表,我可以为每个成员调用print(),所以我
计算向量< A *>。无论如何要获得以下内容
for_each?
vector< A *> :: iterator i;
for(i = list.begin(); i< list.end(); ++ i)(* i) - > print();
Russ
Is there any way to use the STL algorithm for_each if my container
contains pointers? For example,
class A
{
int a;
virtual void print (void) {cout << a << endl;}
};
class B: public A
{
int b;
virtual void print (void) {cout << a << "," << b << endl;}
}
I want to have a list that I can call print() on for each member, so I
figured vector <A*>. Is there anyway to get the following with
for_each?
vector <A*>::iterator i;
for (i=list.begin(); i<list.end(); ++i) (*i)->print();
Russ
推荐答案
" Rusty" < RU ***************** @ shaw.ca>在消息中写道
新闻:b7 ******************************** @ 4ax.com ...
"Rusty" <ru*****************@shaw.ca> wrote in message
news:b7********************************@4ax.com...
如果我的容器
包含指针,有没有办法使用STL算法for_each?例如,
类A
{
int a;
虚拟空白打印(无效){cout<< a<< endl;}
};
B类:公共A
{b / b}
虚拟空白打印(无效){cout<< a<< "," << b<<我希望有一个列表,我可以为每个成员调用print(),所以
我想到了vector< A *> ;.无论如何得到以下内容
for_each?
Is there any way to use the STL algorithm for_each if my container
contains pointers? For example,
class A
{
int a;
virtual void print (void) {cout << a << endl;}
};
class B: public A
{
int b;
virtual void print (void) {cout << a << "," << b << endl;}
}
I want to have a list that I can call print() on for each member, so I figured vector <A*>. Is there anyway to get the following with
for_each?
struct Printer:std :: unary_function< A *,void> {
void operator()(A * a)const {/ *写入cout * /}
}
std :: for_each(list.begin(),list.end(),Printer());
HTH。
Jonathan
struct Printer : std::unary_function<A*, void> {
void operator() (A* a) const { /* write to cout */ }
}
std::for_each(list.begin(), list.end(), Printer());
HTH.
Jonathan
Rusty写道:
如果我的容器
包含指针,有没有办法使用STL算法for_each?例如,
类A
{
int a;
虚拟空白打印(无效){cout<< a<< endl;}
};
B类:公共A
{b / b}
虚拟空白打印(无效){cout<< a<< "," << b<<我希望有一个列表,我可以为每个成员调用print(),所以我想出了矢量< A *>。无论如何要用
for_each获得以下内容?
当然..
struct call_print {
void operator()(A * a){a- >打印(); }
};
然后
for_each(v.begin(),v.end() ,call_print());
vector< A *> :: iterator i;
for(i = list.begin(); i< list.end(); ++ i)(* i) - > print();
别叫你的矢量''列表'',那里已经有一个std :: list类了。
Russ
Is there any way to use the STL algorithm for_each if my container
contains pointers? For example,
class A
{
int a;
virtual void print (void) {cout << a << endl;}
};
class B: public A
{
int b;
virtual void print (void) {cout << a << "," << b << endl;}
}
I want to have a list that I can call print() on for each member, so I
figured vector <A*>. Is there anyway to get the following with
for_each?
Sure..
struct call_print{
void operator()(A* a){ a->print(); }
};
and then
for_each(v.begin(), v.end(), call_print());
vector <A*>::iterator i;
for (i=list.begin(); i<list.end(); ++i) (*i)->print();
Don''t call your vector ''list'', there''s already a std::list class.
Russ
Rusty< ru ***************** @ shaw.ca>写道:
Rusty <ru*****************@shaw.ca> wrote:
如果我的容器
包含指针,有没有办法使用STL算法for_each?例如,
类A
{
int a;
虚拟空白打印(无效){cout<< a<< endl;}
};
B类:公共A
{b / b}
虚拟空白打印(无效){cout<< a<< "," << b<<我希望有一个列表,我可以为每个成员调用print(),所以我想出了矢量< A *>。无论如何得到以下内容
for_each?
vector< A *> :: iterator i;
for(i = list.begin(); i< list.end(); ++ i)(* i) - > print();
Is there any way to use the STL algorithm for_each if my container
contains pointers? For example,
class A
{
int a;
virtual void print (void) {cout << a << endl;}
};
class B: public A
{
int b;
virtual void print (void) {cout << a << "," << b << endl;}
}
I want to have a list that I can call print() on for each member, so I
figured vector <A*>. Is there anyway to get the following with
for_each?
vector <A*>::iterator i;
for (i=list.begin(); i<list.end(); ++i) (*i)->print();
for_each(vec.begin(),vec.end() ,mem_fun(& A :: print));
for_each( vec.begin(), vec.end(), mem_fun( &A::print ) );
这篇关于STL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!