4508: 【例】set-默认less比较器

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:20 解决:12

题目描述

提示

#include<bits/stdc++.h>
using namespace std;
int main(){
    //定义长度为0的set
    //set<int>s;
    int a[]={50,10,30,10,20,40};
    //利用数组初始化 set
	//set<int> s(a,a+6);
	//默认比较器是less<T>:由小到大
	set<int,less<int> > s(a,a+6);
    //插入到特定的位置:插入到特定位置意义不大,因为插入结束后会重新排序
    //s.insert(s.begin(),60);
    s.insert(60); //插入元素60
    //删除set中的第1个元素
	//s.erase(s.begin());
    //删除值为元素 40
    //s.erase(40);
    //遍历set中的元素
    set<int>::iterator it;
    /*
    使用 find 查找元素的位置,返回迭代器如果没有这个元素,返回set.end()
    */ 
    it = s.find(300);
    //*it = 300; //错误,不能直接修改set 中的元素
    if(it != s.end())
        cout<<*it<<"存在!"<<endl;
    else
        cout<<"元素不存在!"<<endl;
    for(it=s.begin();it != s.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}