我正在研究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
是否有這種限制,我找不到答案。我很困惑。