zoukankan      html  css  js  c++  java
  • set的常见用法

    set的常见用法

    #include <set>
    set<int> st;
    set<int>::iterator it; //迭代器
    st.insert(int); //插入一个值
    st.erase(int); // 删除一个值
    st.erase(iterator); //删除迭代器指向的值  
    st.erase(iterator, iterator); //删除两迭代器之间的值
    st.begin(); //返回指向第一个元素的迭代器
    st.end(); //返回指向最后一个元素的迭代器
    st.clear(); //清空
    st.empty(); //判断是否为空
    st.find(int); //返回第一个找到的某一值的迭代器
    st.lower_bound(int); //返回大于等于某一值的第一个元素的迭代器
    st.upper_bound(int); //返回大于某一值的第一个元素的迭代器
    st.size(); //返回元素数量
    
    #include <iostream>
    #include <set>
    using namespace std;
    set<int> st;
    int main() {
    	while(true) {
    		int opt; cin >> opt;
    		if(opt == 1) {
    			int x; cin >> x;
    			st.insert(x);
    		}
    		if(opt == 2) {
    			int x; cin >> x;
    			st.erase(x);
    		}
    		if(opt == 3) {
    			int x; cin >> x;
    			set<int>::iterator p, q;
    			p = st.lower_bound(x);
    			q = st.upper_bound(x);
    			cout << "lower :" << *p << "  upper : " << *q << endl;
    		}
    		if(opt == 4) {
    			int x; cin >> x;
    			set<int>::iterator p;
    			p = st.find(x); cout << *p << endl;
    		}
    		if(opt == 5) {
    			int x; cin >> x;
    			cout << st.count(x) << endl;
    		}
    		if(opt == 6) {
    			cout << "size: " << st.size() << endl;
    		}
    		set<int>::iterator it;
    		cout << "--------- ";
    		for(it = st.begin(); it != st.end(); it++) {
    			cout << (*it) << " ";
    		}
    		cout << " ---------";
    		cout << endl;
    	}
    }
    

    set中重载运算符

    #include <iostream>
    #include <set>
    using namespace std;
    struct node {
    	int fr, se;
    };
    node makenode(int x, int y) {
    	node tmp; tmp.fr = x, tmp.se = y;
    	return tmp;
    }
    bool operator < (const node a, const node b) {
    	if(a.fr == b.fr) return a.se > b.se;
    	return a.fr > b.fr; //降序,以fr为第一关键字,se为第二关键字
    }
    bool operator == (const node a, const node b) { //用于st.find()
    	if(a.fr == b.fr) return true;
    	else return false;
    }
    set<node> st;
    int main() {
    	int tot = 0;
    	while(true) {
    		int opt; cin >> opt; ++tot;
    		if(opt == 1) {
    			int x; cin >> x;
    			st.insert(makenode(x, tot));
    		}
    		if(opt == 2) {
    			int x; cin >> x;
    			set<node>::iterator p = st.find(makenode(x, x));
    			st.erase(p);
    		}
    		set<node>::iterator it;
    		cout << "---------" << endl;
    		for(it = st.begin(); it != st.end(); it++) {
    			cout << (*it).fr << " " << (*it).se << endl;
    		}
    		cout << endl << "---------" << endl;
    	}
    }
    
  • 相关阅读:
    Oracle数据库内置函数
    软件测试工程师笔试题带答案
    JavaBean对象与Map对象互相转化
    Android之MVP模式实现登录和网络数据加载
    AndroidStudio使用偷懒插件Butterknife和GsonFormat
    最强 Android Studio 使用小技巧和快捷键
    android--------Retrofit+RxJava的使用
    Android之封装好的异步网络请求框架
    Android之MVC模式的使用
    Andriod的Http请求获取Cookie信息并同步保存,使第二次不用登录也可查看个人信息
  • 原文地址:https://www.cnblogs.com/mcggvc/p/14364945.html
Copyright © 2011-2022 走看看