如何与reverse_iterator的沿C数组上使用f​​ind_if? [英] How do you use find_if along with reverse_iterator on a C-style array?

查看:107
本文介绍了如何与reverse_iterator的沿C数组上使用f​​ind_if?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要搜索的元素第一次出现在C-磁盘阵列和POD要素,一是便于可以用 STD为此:: find_if(开始,结束,findit) 。但我需要的最后一个实例。 这个答案给我的想法,这可以用的std :: reverse_iterator的。因此,我想:

To search the first occurence of a element in a C-Array with POD elements, one ease can do this with std::find_if(begin, end, findit). But I needed the last occurence. This answer gave me the idea that this can be done with std::reverse_iterator. Thus I tried:

std::find_if(std::reverse_iterator<podtype*>(end),
             std::reverse_iterator<podtype*>(begin),
             findit);

这给我的错误:

不能转换'的std :: reverse_iterator的&LT; XYZ *>'到'赋值XYZ *

cannot convert 'std::reverse_iterator< xyz* > ' to 'xyz*' in assignment

你有一个想法如何做这种方式或者你知道一个更好的解决方案?

Do you have an idea how to do it this way or do you know a better solution?

这是code:

#include <iostream>
#include <iterator>
#include <algorithm>

struct xyz {
    int a;
    int b;
};

bool findit(const xyz& a) {
    return (a.a == 2 && a.b == 3);
}

int main() {
    xyz begin[] = { {1, 2}, {2, 3}, {2, 3}, {3, 5} };
    xyz* end = begin + 4;

    // Forward find
    xyz* found = std::find_if(begin, end, findit);
    if (found != end)
        std::cout << "Found at position "
                  << found - begin
                  << std::endl;

    // Reverse find
    found = std::find_if(std::reverse_iterator<xyz*>(end),
                         std::reverse_iterator<xyz*>(begin),
                         findit);
    if (found != std::reverse_iterator<xyz*>(end));
        std::cout << "Found at position "
                  << found - std::reverse_iterator<xyz*>(end)
                  << std::endl;

    return 0;
}

和在编译器错误codepad.org

推荐答案

的std :: find_if 函数的返回值类型等于迭代器的类型,传过来的参数。在你的情况,因为你正在传递的std :: reverse_iterator的&LT; XYZ *&GT; 取值为参数,返回类型为的std :: reverse_iterator的&LT; XYZ *&GT; 。这意味着

The std::find_if function has a return type equal to the type of iterator passed in as a parameter. In your case, since you're passing in std::reverse_iterator<xyz*>s as parameters, the return type will be std::reverse_iterator<xyz*>. This means that

found = std::find_if(std::reverse_iterator<xyz*>(end),
                     std::reverse_iterator<xyz*>(begin),
                     findit);

将无法编译,因为发现 XYZ *

要解决这个问题,你可以试试这个:

To fix this, you can try this:

std::reverse_iterator<xyz*>
rfound = std::find_if(std::reverse_iterator<xyz*>(end),
                      std::reverse_iterator<xyz*>(begin),
                      findit);

这将解决编译错误。但是,我认为在这行你两个次级错误:

This will fix the compiler error. However, I think that you two secondary errors in this line:

if (found != std::reverse_iterator<xyz*>(end));

首先,请您有如果语句后一个分号,因此如果语句的身体会不管条件是否为真来评价。

First, note that you have a semicolon after the if statement, so the body of the if statement will be evaluated regardless of whether the condition is true.

二,请注意,的std :: find_if 如果没什么predicate匹配,则返回第二个迭代器作为一个定点。因此,这种测试应

Second, note that std::find_if returns the second iterator as a sentinel if the nothing matches the predicate. Consequently, this test should be

if (rfound != std::reverse_iterator<xyz*>(begin))

由于 find_if 将返回的std :: reverse_iterator的&LT; XYZ *&GT;(开始)如果元素不找到。

because find_if will return std::reverse_iterator<xyz*>(begin) if the element is not found.

希望这有助于!

这篇关于如何与reverse_iterator的沿C数组上使用f​​ind_if?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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