一元函数for_each的问题 [英] Problem with unary function for_each

查看:65
本文介绍了一元函数for_each的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

只是想再次感谢你过去的帮助。我有一个新的

问题,我想知道是否有任何人可以帮助我。


i写了这个abtract类形状及其派生类圈,

矩形和三角形。我还为每个形状写了这个方法

对象叫做''draw_all_seq_inside2'',它有两个值。我想

在另一种方法中使用for_each语句调用此方法,但它似乎无法工作。这是代码的修剪版本。我需要使用for_each语句来做

,因为它是一个课程。请看看

并回复给我(对不起冗长的代码,但我尽可能多地修剪它们

)。


#include< vector>

#include< list>

#include< deque>

#include< iostream> ;

#include< string>

#include< complex>

#include< functional>

#include< algorithm>


使用命名空间std;


class Shape {

protected:

string shapename;

public:

Shape(字符串名称):shapename(name){};

virtual void draw()const = 0;


virtual void inside_window2(complex< double>& bl,complex< double>

& tr)const = 0 ;

};


class Circle:public Shape {

public:

const double x1 ;

const double y1;

const double radius;


Circle(复数< double> first,double rad):Shape(Circle),

x1(first.real()),y1(first.imag()),radius(rad){}


void draw()const {

complex< double> first(x1,y1);

cout<< "<式样::" << Shape :: shapename<< " " <<第一个<< "

<<半径

<< " > \ n" ;;

}


void inside_window2(复杂< double>& bl,复杂< double>& tr)const

{

if((x1 - radius)> = bl.real()&&(y1 - radius)> = bl.imag()&& ;

(x1 +半径)< = tr.real()&&(y1 + radius)< = tr.imag()){

draw ();

}

}

};


类Rectangle:public Shape {

public:

const double x1;

const double y1;

const double x2;

const double y2;


矩形(复数< double>首先,复数< double>秒):

Shape(" Rectangle"), x1(first.real()),y1(first.imag()),

x2(second.real()),y2(second.imag()){}


void draw()const {

complex< double> first(x1,y1);

complex< double>秒(x2,y2);

cout<< "<式样::" << Shape :: shapename<< " " <<第一个<< "

<<第二个

<< " > \ n" ;;

}


void inside_window2(复杂< double>& bl,复杂< double>& tr)const

{

if(bl.real()< = x1&& bl.imag()< = y1&& tr.real()> = x2&&

tr.imag()> = y2){

draw();

}


}


};


class Triangle:public Shape {

public :

const double x1;

const double y1;

const double x2;

const double y2;

const double x3;

const double y3;


Triangle(复数< double> first,complex< double> second,

complex< double> third):

Shape(" Triangle"),x1(first.real()),y1(first.imag()),

x2(second.real()),

y2(second.imag()),x3(third.real()),y3(third.imag()){ }


void draw()const {

complex< double> first(x1,y1);

complex< double>秒(x2,y2);

complex< double>第三(x3,y3);

cout<< "<式样::" << Shape :: shapename<< " " <<第一个<< "

<<第二个

<< " " <<第三<< " > \ n" ;;

}


void inside_window2(复杂< double>& bl,复杂< double>& tr)const

{

double wx1 = bl.real();

double wy1 = bl.imag();

double wx2 = tr.real();

double wy2 = tr.imag();

if(wx1< = x1&& wx1< = x2& ;& wx1< = x3&& wy1< = y1&& wy1< =

y2&& wy1< = y3&&

wx2> = x1&& wx2> = x2&& wx2> = x3&& wy2> = y1&& wy2> =

y2&& wy2> = y3){

draw();

}

}
< br $>
};

模板< typename容器>

void draw_all_seq_inside2(Container& c,Rectangle& w){


complex< double>首先(w.x1,w.y1);

complex< double>第二个(w.x2,w.y2);


for_each(c.begin(),c.end(),mem_fun(& Shape :: inside_window2(first,

秒)));


}


int main(){

vector< ;形状*>形状;

complex< double> first(0,0);

complex< double> secondr(1,1);

complex< double> secondt(0,1);

complex< double>第三个(1,0);


圆圈c =圆圈(第一个,1个);

矩形r =矩形(第一个,第二个);

三角形t =三角形(第一个,第二个,第三个);

shapes.push_back(& c);

shapes.push_back(& r) ;

shapes.push_back(& t);


draw_all_seq_inside2(形状,r);


返回0;

}

hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.

i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called ''draw_all_seq_inside2'' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).

#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <string>
#include <complex>
#include <functional>
#include <algorithm>

using namespace std;

class Shape {
protected:
string shapename;
public:
Shape(string name): shapename(name) {};
virtual void draw() const = 0;

virtual void inside_window2(complex<double> &bl, complex<double>
&tr) const = 0;
};

class Circle : public Shape {
public:
const double x1;
const double y1;
const double radius;

Circle(complex<double> first, double rad): Shape("Circle"),
x1(first.real()), y1(first.imag()), radius(rad) {}

void draw() const {
complex<double> first(x1, y1);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< radius
<< " >\n";
}

void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if((x1 - radius) >= bl.real() && (y1 - radius) >= bl.imag() &&
(x1 + radius) <= tr.real() && (y1 + radius) <= tr.imag()){
draw();
}
}
};

class Rectangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;

Rectangle(complex<double> first, complex<double> second):
Shape("Rectangle"), x1(first.real()), y1(first.imag()),
x2(second.real()), y2(second.imag()) {}

void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " >\n";
}

void inside_window2(complex<double> &bl, complex<double> &tr) const
{
if(bl.real() <= x1 && bl.imag() <= y1 && tr.real() >= x2 &&
tr.imag() >= y2){
draw();
}

}

};

class Triangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
const double x3;
const double y3;

Triangle(complex<double> first, complex<double> second,
complex<double> third):
Shape("Triangle"), x1(first.real()), y1(first.imag()),
x2(second.real()),
y2(second.imag()), x3(third.real()), y3(third.imag()) {}

void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
complex<double> third(x3, y3);
cout << "<Shape::" << Shape::shapename << " " << first << " "
<< second
<< " " << third << " >\n";
}

void inside_window2(complex<double> &bl, complex<double> &tr) const
{
double wx1 = bl.real();
double wy1 = bl.imag();
double wx2 = tr.real();
double wy2 = tr.imag();
if(wx1 <= x1 && wx1 <= x2 && wx1 <= x3 && wy1 <= y1 && wy1 <=
y2 && wy1 <= y3 &&
wx2 >= x1 && wx2 >= x2 && wx2 >= x3 && wy2 >= y1 && wy2 >=
y2 && wy2 >= y3){
draw();
}
}

};
template <typename Container>
void draw_all_seq_inside2(Container& c, Rectangle &w){

complex<double> first(w.x1, w.y1);
complex<double> second(w.x2, w.y2);

for_each(c.begin(), c.end(), mem_fun(&Shape::inside_window2(first,
second)));

}

int main() {
vector<Shape *> shapes;
complex<double> first(0, 0);
complex<double> secondr(1, 1);
complex<double> secondt(0, 1);
complex<double> third(1, 0);

Circle c = Circle(first, 1);
Rectangle r = Rectangle(first, secondr);
Triangle t = Triangle(first, secondt, third);
shapes.push_back(&c);
shapes.push_back(&r);
shapes.push_back(&t);

draw_all_seq_inside2(shapes, r);

return 0;
}

推荐答案

fr ******* @ hotmail.com 写道:
[..]
我写过这个abtract类形状及其派生类圈,矩形和三角形。我还为每个形状的对象编写了这个方法,称为''draw_all_seq_inside2'',它有两个值。我想在另一种方法中使用for_each语句来调用此方法,但它似乎不起作用。这是代码的修剪版本。我需要使用for_each语句来做这个,因为它是一个课程。请看看
并回复我(对于冗长的代码感到抱歉,但我尽可能地修剪它。)
[..]
[..]
i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called ''draw_all_seq_inside2'' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).
[..]




请RTFM。 std :: for_each只能调用一个带有一个参数的函数

,该参数应该是取消引用迭代器的结果(或者

可以从解引用的结果中转换一个迭代器)。 IOW,'std :: for_each''实现'std :: for_each''是这样的:


模板< class I,F级>

F my_for_each(I i1,I i2,F f)

{

while(i1!= i2)

f(* i1 ++ );

返回f;

}


如果您阅读本文并尝试了解它的作用,您会看到你需要提供一个_function pointer_或_functor_作为第三个参数

''for_each''。 ''mem_fun''是一个适配器。它需要一个_no-argument_成员

或一个_single-argument_成员函数,并创建一个单参数的仿函数

或一个双参数仿函数。你的是一个_two-argument_成员

函数(它实际上有三个参数,第一个是隐藏对象

指针或引用,里面变成''this'')。如果你想用它来使用

''mem_fun'',你还必须使用''bind2nd''来传递成员函数的

第二个参数。这使得这个例子比你准备解决的更复杂。研究更多关于模板和关于

''bind1st''和''bind2nd''活页夹和''mem_fun *''适配器。


如果有人写道对你而言,我敢打赌,你真的不会学到很多关于这些机制的b $ b。如果你想学习,你必须自己动手。


有很多使用''bind1st''和''bind2nd''以及
的例子档案中
''mem_fun''。我可以写另一个,但我不是真的

看点。


V



Please RTFM. std::for_each is can only call a function with one argument
and that argument should be the result of dereferencing the iterator (or
one convertible from the result of dereferencing the iterator). IOW, one
possible implementation of ''std::for_each'' is this:

template<class I, class F>
F my_for_each(I i1, I i2, F f)
{
while (i1 != i2)
f(*i1++);
return f;
}

If you read this and try to understand what it does, you will see that you
need to supply a _function pointer_ or a _functor_ as the third argument
of ''for_each''. ''mem_fun'' is an adapter. It takes a _no-argument_ member
or a _single-argument_ member function and makes a one-argument functor
or a two-argument functor out of it. Yours is a _two-argument_ member
function (which really has three arguments, the first is the hidden object
pointer or reference which inside becomes ''this''). If you want to use
''mem_fun'' with it, you have to also use ''bind2nd'' with it to pass the
second argument of your member function. That makes the example more
complex than you''re ready to tackle. Study more about templates and about
''bind1st'' and ''bind2nd'' binders and ''mem_fun*'' adapters.

If somebody writes it for you, I bet you''re not really going to learn much
about those mechanisms. If you want to learn, you have to do it yourself.

There are plenty of examples of using ''bind1st'' and ''bind2nd'' and also of
''mem_fun'' in the archives. I could write another one, but I don''t really
see the point.

V


在文章< 11 ********************** @ z14g2000cwz.googlegroups .com>,
fr ******* @ hotmail.com 写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
fr*******@hotmail.com wrote:
只是想再次感谢过去的帮助。我有一个新的问题,我想知道是否有任何身体可以帮助我。

我写过这个abtract类形状及其派生类圈,
矩形和三角形。我还为每个形状的对象编写了这个方法,称为''draw_all_seq_inside2'',它有两个值。我想在另一种方法中使用for_each语句来调用此方法,但它似乎不起作用。这是代码的修剪版本。我需要使用for_each语句来做这个,因为它是一个课程。请看看
并回复我(对于冗长的代码感到抱歉,但我尽可能地修剪它。)
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.

i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called ''draw_all_seq_inside2'' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).




我修改你的代码:


class Shape {

public:

virtual void inside_window2(complex< double>& bl,

complex< double>& tr)const = 0;

};


void foo(vector< Shape *> c,

complex< double> x,complex< double> y)

{

for_each(c.begin(), c.end(),/ *在每个形状上用参数x和y * /)调用

inside_window2的东西;

}

执行此类操作的最简单方法是将循环体

放入仿函数中,然后创建一个并将其传递给for_each函数...


因此,循环内部如下所示:

void inside_loop(const Shape * s,complex< double> x,complex< double> y )

{
s-> inside_window_2(x,y);

}


但我们要提供''x''并且''y''的时间与''s'不同。

这需要两个步骤,并且必须存储x和y。 。


struct call_inside_window2_with {

complex< double> x,y;

foo(复数< double> a,复数< double> b):x(a),y(b){}

void operator() (const Shape * s)const {

s-> inside_window2(x,y);

}

};


for_each(c.begin(),c.end(),call_inside_window2_with(x,y));



I trimmed your code a bit more:

class Shape {
public:
virtual void inside_window2(complex<double> &bl,
complex<double> &tr) const = 0;
};

void foo( vector<Shape*> c,
complex<double> x, complex<double> y )
{
for_each( c.begin(), c.end(), /* something that will call
inside_window2 on each shape with the arguments x and y */ );
}

The simplest way to do this sort of thing is to put the body of the loop
in a functor then create one and pass it to the for_each function...

So, the inside of the loop looks like this:

void inside_loop( const Shape* s, complex<double> x, complex<double> y )
{
s->inside_window_2( x, y );
}

but we want to provide the ''x'' and ''y'' at a different time than the ''s''.
That requires two steps, and the ''x'' and ''y'' have to be stored...

struct call_inside_window2_with {
complex<double> x, y;
foo( complex<double> a, complex<double> b ): x(a), y(b) { }
void operator()( const Shape* s ) const {
s->inside_window2( x, y );
}
};

for_each( c.begin(), c.end(), call_inside_window2_with( x, y ) );


我在哪里放以下代码让for_each工作。

i无法弄明白。请使用原始文件作为模板

给出一个例子。


struct call_inside_window2_with {

complex< double> x,y;

foo(复数< double> a,复数< double> b):x(a),y(b){}

void operator() (const Shape * s)const {

s-> inside_window2(x,y);

}


};

where would i put the following piece of code for the for_each to work.
i cant figure it out. please use the original file as a template of
give an example.

struct call_inside_window2_with {
complex<double> x, y;
foo( complex<double> a, complex<double> b ): x(a), y(b) { }
void operator()( const Shape* s ) const {
s->inside_window2( x, y );
}

};


这篇关于一元函数for_each的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆