set容器中的find,不是如果找不到对应的元素的话就会返回end()吗,但是为什么cout<<*(s1.find(3));这行代码会使程序崩溃,
理论上他应该会返回22才对呀
#include
using namespace std;
#include
void test4()
{
//查找和统计
set<int>s1;
s1.insert(10);
s1.insert(12);
s1.insert(0);
s1.insert(1);
s1.insert(13);
s1.insert(22);
s1.insert(4);
s1.insert(11);
//s1.find(key)
//查找key存不存在,若存在返回该元素的迭代器,如不存在返回set.end();
//s1.count(key)
// 查找key的个数
cout<<*(s1.find(3));
}
int main()
{
test4();
system("pause");
return 0;
}
end()类似文件的EOF一样,是个无效标志,你用*取地址当然报错了
你需要先判断find结果不等于end(),然后才能输出
尝试访问set::end()
迭代器所指的元素是undefined behavior.
https://en.cppreference.com/w/cpp/container/set/end
Returns an iterator to the element following the last element of the set.
This element acts as a placeholder; attempting to access it results in undefined behavior.