STL 算法和 const_iterators

发布于 2024-08-09 06:45:52 字数 499 浏览 3 评论 0原文

今天我写了一个小谓词来查找容器中的匹配符号。

但我面临一个问题:我想在类的 const 方法内的 std::find_if 调用中使用此谓词,在作为此类成员的容器中搜索。

但我刚刚注意到 std::findstd::find_if 都无法在 const_iterators 上操作!

我检查了一些 C++ 参考文献,似乎没有接受/返回 const_iterators 的 std::findstd::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

快乐很简单 2024-08-16 06:45:52

std::findstd::find_if 绝对可以对给定容器的 *::const_iterator 进行操作。您是否偶然查看了这些函数的签名并误解了它们?

template <class InputIterator, class Type>
InputIterator find(InputIterator first, InputIterator last, const Type& val);

请注意,这里的InputIterator只是一个模板类型参数的名称,任何const_iterator都可以满足它的要求。

或者,也许您将 const_iterator(即引用 const 值的迭代器)与 const 迭代器(即本身就是 const 的迭代器)混淆了>)?

std::find and std::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?

template <class InputIterator, class Type>
InputIterator find(InputIterator first, InputIterator last, const Type& val);

Note that InputIterator here is just a name of a template type parameter, and any const_iterator will satisfy the requirements for it.

Or, perhaps, you're confusing const_iterator (i.e. an iterator referencing a const value) with a const iterator (i.e. an iterator which is itself const)?

身边 2024-08-16 06:45:52

std::findstd::find_if 都将迭代器类型作为模板参数,因此它们肯定可以进行操作const_iterators。仅举一个简单的例子:

#include <vector>
#include <algorithm>
#include <iostream>
int main() { 
    std::vector<int> x;

    std::fill_n(std::back_inserter(x), 20, 2);
    x.push_back(3);

    std::vector<int>::const_iterator b = x.begin();
    std::vector<int>::const_iterator e = x.end();

    std::vector<int>::const_iterator p = std::find(b, e, 3);

    std::cout << *p << " found at position: " << std::distance(b, p) << "\n";
    return 0;
}

这应该被任何正常运行的 C++ 编译器接受,并产生如下结果:

3 在位置:20 找到

std::find and std::find_if both take the iterator type as a template parameter, so they most certainly can operate on const_iterators. Just for a quick example:

#include <vector>
#include <algorithm>
#include <iostream>
int main() { 
    std::vector<int> x;

    std::fill_n(std::back_inserter(x), 20, 2);
    x.push_back(3);

    std::vector<int>::const_iterator b = x.begin();
    std::vector<int>::const_iterator e = x.end();

    std::vector<int>::const_iterator p = std::find(b, e, 3);

    std::cout << *p << " found at position: " << std::distance(b, p) << "\n";
    return 0;
}

This should be accepted by any properly functioning C++ compiler, and produce results like:

3 found at position: 20

來不及說愛妳 2024-08-16 06:45:52

如果万一你来这里的原因和我一样:

error: no matching function for call to ‘find(std::vector<int>::const_iterator, std::vector<int>::const_iterator, int)’

它与 const_iterator 没有任何关系。您可能只是忘记了#include :-)

If by any chance you are here for the same reason as me:

error: no matching function for call to ‘find(std::vector<int>::const_iterator, std::vector<int>::const_iterator, int)’

It doesn't have anything to do with const_iterators. You probably just forgot to #include <algorithm> :-)

寄与心 2024-08-16 06:45:52

我刚刚遇到了同样的问题。我有一个在成员向量上调用 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 function const. It turned out that this was because I was assigning the return value of find_if to an iterator instead of const_iterator. The caused the compiler to assume that the parameters of find_if must also be iterator instead of const_iterator, which it could not get from the const member vector.

多情出卖 2024-08-16 06:45:52

我刚刚遇到此代码的问题:

std::string str;
std::string::const_iterator start = str.begin();
std::string::const_iterator match = std::find(start, str.end(), 'x');

错误是“std::find 没有匹配的重载”。

我需要的修复是使用 cend()。令人困惑的是 cbegin() 不是必需的,我不确定为什么该转换可以(隐式)而不是 end() 作为函数参数。

I just had an issue with this code:

std::string str;
std::string::const_iterator start = str.begin();
std::string::const_iterator match = std::find(start, str.end(), 'x');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文