C++ 在c+中查找函数+&书信电报;算法>;
我在Ubuntu 18.04.2 LTS上使用g++7.4.0 我想我发现了一个bug:C++ 在c+中查找函数+&书信电报;算法>;,c++,stl,find,C++,Stl,Find,我在Ubuntu 18.04.2 LTS上使用g++7.4.0 我想我发现了一个bug: vector<int> a(10,1); vector<int>::iterator it = find(a.begin()+6,a.begin()+3,8); if(it != a.end()) cout<<"FOUND"<<endl; 应该找不到吗?我还发现: it = a.begin()+3 不,这不是一个错误。它完全符合标准定义: 从 设E为: -
vector<int> a(10,1);
vector<int>::iterator it = find(a.begin()+6,a.begin()+3,8);
if(it != a.end()) cout<<"FOUND"<<endl;
应该找不到吗?我还发现:
it = a.begin()+3
不,这不是一个错误。它完全符合标准定义: 从 设E为:
-
*i==value
用于查找,
返回:范围[first
,last
)中E为true
的第一个迭代器i
。如果未找到此类迭代器,则返回last
由于[a.begin()+6
,a.begin()+3
)范围内没有*i==8
为真的迭代器i
,std::find
返回a.begin()+3
如果第一个>最后一个,这是未定义的行为吗?@Kevin我找不到任何前提条件来说明这一点。它完全可能在那里,我错过了它。所以中的算法是错误的?它应该是while(first
而不是while(first!=last)
@Duy它返回范围[first,last)
中的第一个迭代器。find的迭代器是\u InputI迭代器,只能进行相等比较。因为您的范围是相反的(其中lastit = a.begin()+3