Set用于存储不重复的元素,若重复存入同一元素,则set会将其覆盖掉。
#include <iostream>
#include <set>
using namespace std;
set <int> h;//这个地方的int可以换为任意你想储存元素的类型
int main() {
int n, x;
cin >> n;
h.clear();//这条语句的目的是清理h;
for(int i = 0; i < n; i++) {
cin >> x;
h.insert(x);
}
set <int>::iterator it;
for(it = h.begin(); it != h.end(); it++)
cout << (*it) << endl;
if(h.find(3) != h.end()) cout << "Yes, 3 is in set h" << endl;
else cout << "No, element does not exist" << endl;
return 0;
}