自学内容网 自学内容网

set的相关函数(2)

2.查找

//查找与删除
/*
int main()
{
//去重+默认升序排列
set<int> s;
s.insert(2);
s.insert(4);
s.insert(3);
s.insert(2);
s.insert(5);
s.insert(9);
//迭代器遍历
//set<int, greater<int>>::iterator it = s.begin();
auto it = s.begin();
while (it != s.end())
{
//*it = 1;---->error
//不可以修改
cout << *it << " ";
++it;
}
cout << endl;

int x = 0;
cout << "请输入你要查找的数字:";
cin >> x;
//如果查找不到就会返回迭代器的尾部
auto pos = s.find(x);
if (pos != s.end())
{
cout << x << "存在" << endl;
}
else
{
cout << x << "不存在" << endl;
}

//使用count来间接查找
//count可以统计元素出现的次数,如果出现0次就不存在,反之则存在

if (s.count(x))
{
cout << x << "存在" << endl;
}
else
{
cout << x << "不存在" << endl;
}

return 0;
}
*/

 


原文地址:https://blog.csdn.net/2301_80689220/article/details/142444926

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!