Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将此for循环更改为for_each循环?_C++ - Fatal编程技术网

C++ 如何将此for循环更改为for_each循环?

C++ 如何将此for循环更改为for_each循环?,c++,C++,如何将其转换为循环 对于_each(): 模板 每个_的一元函数(先输入,后输入,一元函数fn) { while(第一个!=最后一个) fn(*first++);//首先递增,但取消引用其旧值。 返回fn; } 只是一个猜测,但我要说的是,inside_window不是一个常量方法,这就是为什么会出现这种错误。你能给我们看一下密码吗 您已声明p为const,这意味着您承诺不会使用它更改任何内容。但是你的inside_window()方法并没有承诺不这样做,因此编译器说你没有遵守这个承诺。使inn

如何将其转换为循环

对于_each():

模板
每个_的一元函数(先输入,后输入,一元函数fn)
{
while(第一个!=最后一个)
fn(*first++);//首先递增,但取消引用其旧值。
返回fn;
}

只是一个猜测,但我要说的是,inside_window不是一个常量方法,这就是为什么会出现这种错误。你能给我们看一下密码吗


您已声明p为const,这意味着您承诺不会使用它更改任何内容。但是你的inside_window()方法并没有承诺不这样做,因此编译器说你没有遵守这个承诺。使inner\u window const,这应该会消失(如果您确实没有在该方法中更改任何内容,那么这将是一个简单的修复)

您需要将
矩形::inner\u window()
声明为
const
方法:

virtual bool inside_window(const std::complex&, const std::complex&) const;
                                                                  // ^^^^^
这使得
This
的类型为
const rectangle*
,而不仅仅是
rectangle*
,它允许对
const rectangle
调用
inner\u window()
,因为它必须位于
for each()

但是,您的逻辑是有缺陷的:如果您想测试
内部\u window()
的结果并有条件地调用
draw()
,那么使用
for\u each()
的唯一方法就是使用一个助手方法,或者作为一个函子:

struct draw_visible : public std::unary_function<rectangle, void> {

    const std::complex<double> bl, tr;

    draw_visible(const std::complex<double>& bl, const std::complex<double> tr)
        : bl(bl), tr(tr) {}

    void operator()(const rectangle& r) const {
        if (r.inside_window(bl, tr))
            r.draw();
    }

};

template<class Container>
void draw_all(const Container& c, const rectangle& w) {
     std::for_each(c.begin(), c.end(), draw_visible(w.get_bl(), w.get_tr()));
}

for_的第三个参数需要是一个函数。更具体地说,是一个函数,它接收
*p
,并为您的循环执行该函数体

/* function or method */ (const Container& c){
   if(c->inside_window(cl, tr))
     c->draw();
}
但是请注意我们需要如何跟踪out函数的
cl
tr
。完成这项工作的方法是使函数成为对象的
操作符()
重载,该对象将
cl
tr
作为实例变量

class Drawer{
    complex<double> bl, tr;
    Drawer(/*constructor parameters, probably w*/){ /*...*/}
    void operator() (const Container& c){ /*...*/ }
};

for_each(c.begin(), c.end(), Drawer(w));
类抽屉{
复合bl,tr;
抽屉(/*构造函数参数,可能是w*/){/*…*/}
void运算符()(常量容器&c){/*…*/}
};
对于每个(c.开始()、c.结束()、抽屉(w));

rectangle::inside_window()是否声明为const?@jzila我已经添加了inside_window()是,正如我所怀疑的那样。它必须是const,但是在你回复我的评论之前,其他人已经给了你这个答案;否则返回true(只需
return!x;
就足够了)并且
cout.flush()
在该方法中没有意义。不是你的错,但是…我有点可怜你。在定义的末尾,在原来的postadd const中添加了inside_window;看Jon Purdy的答案,我不明白。你们都画了些什么?我已将我的for_添加到原始帖子中。draw_可视应该是在draw_all()中吗?对不起,我不明白这一点,你能解释一下我是一个beginner@user521180:呃,你还不明白什么?第一段代码。。那是从哪里来的?为什么签名中没有矩形?我不明白我必须做什么来修复它在第一位代码中,
是伪代码,对不起。矩形不在签名中,因为for_每个将只传递一个与循环迭代器对应的参数。这就是为什么我们必须将函数转换为一个方法,并将与矩形相关的内容转换为实例变量(这样就可以在不作为参数传递的情况下访问它们)。
struct draw_visible : public std::unary_function<rectangle, void> {

    const std::complex<double> bl, tr;

    draw_visible(const std::complex<double>& bl, const std::complex<double> tr)
        : bl(bl), tr(tr) {}

    void operator()(const rectangle& r) const {
        if (r.inside_window(bl, tr))
            r.draw();
    }

};

template<class Container>
void draw_all(const Container& c, const rectangle& w) {
     std::for_each(c.begin(), c.end(), draw_visible(w.get_bl(), w.get_tr()));
}
template<class Container>
void draw_all(const Container& c, const rectangle& w) {
    std::for_each(c.begin(), c.end(), [&w](const rectangle& r) {
        if (r.inside_window(w.get_bl(), w.get_tr())
            r.draw();
    });
}
// A basic mutable 2D double vector.
struct point {
    double x, y;
};
/* function or method */ (const Container& c){
   if(c->inside_window(cl, tr))
     c->draw();
}
class Drawer{
    complex<double> bl, tr;
    Drawer(/*constructor parameters, probably w*/){ /*...*/}
    void operator() (const Container& c){ /*...*/ }
};

for_each(c.begin(), c.end(), Drawer(w));