我正在研究std::search
(以确定std::set
之间是否存在重复项),但我不理解输出结果。C++ std:搜索行为或限制
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
set<int> a{9,10,11,12};
set<int> b{11,12,13};
auto it = search(a.begin(), a.end(), b.begin(), b.end());
if (it != a.end())
cout << "Common is " << *it << endl;
else
cout << "Oops " << *it << endl;
return 0;
}
因此,我希望*it
是11,但事实证明it!=a.end()
失败,*it
打印一些无关痛痒的价值(4在这里),我想我可能会搞砸。
但是,当我将b分配到{11,12}
时,一切都按预期工作并打印出"Common is 11"
。经过多次尝试后,我再也看不到这种模式。我不知道std::search
是否有这种限制,我找不到答案。我很困惑。