gpt4 book ai didi

c++ - 在 C++ 中查找函数

转载 作者:行者123 更新时间:2023-11-30 04:46:49 25 4
gpt4 key购买 nike

我在 Ubuntu 18.04.2 LTS 上使用 g++ 7.4.0

我想我发现了一个错误:

vector<int> a(10,1);
vector<int>::iterator it = find(a.begin()+6,a.begin()+3,8);
if(it != a.end()) cout<<"FOUND"<<endl;

结果:

FOUND

它应该找不到吗?我还发现:

it = a.begin()+3

最佳答案

不,这不是错误。它完全遵循标准定义:

来自 [alg.find]

Let E be:
*i == value for find,
...

Returns: The first iterator i in the range [first, last) for which E is true. Returns last if no such iterator is found.

由于范围 [a.begin()+6, a.begin()+3) 中没有迭代器 i *i == 8 成立,std::find 返回 a.begin()+3

关于c++ - 在 C++ 中查找函数 <algorithm>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com