STL 算法和 const_iterators
今天我写了一个小谓词来查找容器中的匹配符号。
但我面临一个问题:我想在类的 const 方法内的 std::find_if 调用中使用此谓词,在作为此类成员的容器中搜索。
但我刚刚注意到 std::find
和 std::find_if
都无法在 const_iterators
上操作!
我检查了一些 C++ 参考文献,似乎没有接受/返回 const_iterators 的 std::find
或 std::find_if
版本。我只是不明白为什么,因为从我所看到的来看,这些算法无法修改迭代器引用的对象。
以下是 SGI 实现中 std::find
的记录方式:
返回第一个迭代器 i 范围 [第一个,最后一个)使得 *i == 价值。如果没有则返回最后一个 迭代器存在。
Today I wrote a small predicate to find matching symbols in a container.
But I'm faced to a problem: I want to use this predicate in a std::find_if
call inside a const-method of a class, searching in a container that is a member of this class.
But I just noticed that neither std::find
nor std::find_if
are able to operate on const_iterators
!
I checked on some C++ references and it seems there is no version of std::find
or std::find_if
that accept/return const_iterators
. I just can't understand why, since from what I've seen, there is no way that these algorithms could modify the object referenced by the iterator.
Here is how is documented std::find
in the SGI implementation:
Returns the first iterator i in the
range [first, last) such that *i ==
value. Returns last if no such
iterator exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
std::find
和std::find_if
绝对可以对给定容器的*::const_iterator
进行操作。您是否偶然查看了这些函数的签名并误解了它们?请注意,这里的
InputIterator
只是一个模板类型参数的名称,任何const_iterator
都可以满足它的要求。或者,也许您将 const_iterator(即引用 const 值的迭代器)与 const 迭代器(即本身就是 const 的迭代器)混淆了>)?
std::find
andstd::find_if
can definitely operate on*::const_iterator
for a given container. Are you by chance looking at the signatures of those functions, and misunderstanding them?Note that
InputIterator
here is just a name of a template type parameter, and anyconst_iterator
will satisfy the requirements for it.Or, perhaps, you're confusing
const_iterator
(i.e. an iterator referencing a const value) with aconst
iterator (i.e. an iterator which is itselfconst
)?std::find
和std::find_if
都将迭代器类型作为模板参数,因此它们肯定可以对进行操作const_iterators
。仅举一个简单的例子:这应该被任何正常运行的 C++ 编译器接受,并产生如下结果:
3 在位置:20 找到
std::find
andstd::find_if
both take the iterator type as a template parameter, so they most certainly can operate onconst_iterators
. Just for a quick example:This should be accepted by any properly functioning C++ compiler, and produce results like:
3 found at position: 20
如果万一你来这里的原因和我一样:
它与 const_iterator 没有任何关系。您可能只是忘记了
#include
:-)If by any chance you are here for the same reason as me:
It doesn't have anything to do with
const_iterator
s. You probably just forgot to#include <algorithm>
:-)我刚刚遇到了同样的问题。我有一个在成员向量上调用
find_if
的成员函数,当我尝试使成员函数const
时,编译器给了我一个错误。事实证明,这是因为我将find_if
的返回值分配给iterator
而不是const_iterator
。这导致编译器假设find_if
的参数也必须是iterator
而不是const_iterator
,它无法从获取该参数。 const
成员向量。I have just had the same problem. I had a member function that was calling
find_if
on a member vector, and the compiler gave me an error when I tried making the member functionconst
. It turned out that this was because I was assigning the return value offind_if
to aniterator
instead ofconst_iterator
. The caused the compiler to assume that the parameters offind_if
must also beiterator
instead ofconst_iterator
, which it could not get from theconst
member vector.我刚刚遇到此代码的问题:
错误是“std::find 没有匹配的重载”。
我需要的修复是使用 cend()。令人困惑的是 cbegin() 不是必需的,我不确定为什么该转换可以(隐式)而不是 end() 作为函数参数。
I just had an issue with this code:
The error was "no matching overload for std::find".
The fix I needed was to use cend(). It's confusing that cbegin() isn't required, I'm not sure why that conversion is okay (implicitly) and not for the end() as a function parameter.