且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何与reverse_iterator的沿C数组上使用f​​ind_if?

更新时间:2023-11-28 11:28:46

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

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.

希望这有助于!