考虑一下这段代码:
我的印象是,范围的调用约定应该是对应原始算法的一对一映射(即,只需跳过`.begin()`和`.end()`,就可以像以前一样工作)。
显然,在这里并非如此。我错过了什么吗?
代码链接: https://godbolt.org/z/3a6z9c5of
#include <vector>
#include <iostream>
#include <cstdint>
#include <ranges>
int main()
{
struct S {
int a;
int b;
bool operator==(int other) const { return a == other; }
};
std::vector<S> iv{
{1, 2},
{3, 4}
};
// this works
if (auto const it{std::find(iv.begin(), iv.end(), 1)}; it != iv.end()) {
std::cout << "Found!\n" << "\n";
}
//std::ranges::find(iv, 2); // <-- why does this not compile
}
我的印象是,范围的调用约定应该是对应原始算法的一对一映射(即,只需跳过`.begin()`和`.end()`,就可以像以前一样工作)。
显然,在这里并非如此。我错过了什么吗?
代码链接: https://godbolt.org/z/3a6z9c5of