您的位置:首页 > 其它

STL之<set>

2012-07-23 19:19 162 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: