STL里set函数的基本应用
来源:互联网 发布:vmware mac os 10.12 编辑:程序博客网 时间:2024/05/16 09:10
POJ -2340: SB_cyh and his BST one
题面链接
- 题意 :
给定一些要求如(插入,删除,输出,查找前驱或后继,清空,计数,查找等)通过STL里set的自带函数完成相应要求。
- 分析
该题是STL里set的经典练习,对初学STL的同学熟悉set里的函数有很大的帮助。
- 代码
#include<stdio.h>#include<string.h>#include<algorithm>#include<bits/stdc++.h>using namespace std;int main(){ int x; char a[22]; set<int>s;//定义int型集合s。(set可以是任何类型)往set里丢的数如果有相同的会保留一个,如果允许有相同的数,那么使用multiset.往set里丢的数默认从小到大排序的 set<int>::iterator it;//迭代器it相当于一个指针 while(~scanf("%s",a)) { if(a[0]=='-') break; if(a[0]=='i') { scanf("%d",&x); s.insert(x);//insert()函数,代表插入某个数,如果该数已存在,忽略这一操作 } if(a[0]=='d') { scanf("%d",&x); if(s.count(x)==0)//对x计数,如果个数为零表示未查找到 printf("Input Error\n"); else s.erase(x);//erase()函数是删除函数 } if(a[0]=='s') { scanf("%d",&x); it=s.find(x);//查找x的地址附给指针it if(s.count(x)==0) printf("Input Error\n"); else { it++; if(it==s.end())//set里最后一位的下一为s.end() printf("%d is the maximum\n",x); else { it=s.upper_bound(x);//s.upper_bound(x)返回大于x元素且与x最接近的数的迭代器 printf("The successor of %d is %d\n",x,*it); } } } if(a[0]=='p'&&a[2]=='e') { scanf("%d",&x); it=s.find(x); if(s.count(x)==0) printf("Input Error\n"); else { if(it==s.begin()) printf("%d is the minimum\n",x); else { it=s.lower_bound(x);//返回指向大于(或等于)某值的第一个元素的迭代器 it--;//由于该数x在set里存在所以it指向的为该数x printf("The predecessor of %d is %d\n",x,*it); } } } if(a[0]=='p'&&a[2]=='i') { for(it=s.begin();it!=s.end();it++)//*s.begin()等于set里的第一个数,*s.end()==s.size()集合中元素的数目; printf("%d,",*it);//it是指针*it表示指针指向的数 printf("end of print\n"); } if(a[0]=='e') { printf("end of this test\n"); s.clear();//清除set里所有元素 } } return 0;}
0 0
- STL里set函数的基本应用
- STL set的基本操作
- STL的set基本用法
- STL之Set:Set的基本用法
- STL之Set:Set的基本用法
- STL之Set:Set的基本用法
- STL Set的综合应用
- 认知【STL】里的set和multise
- set函数的应用
- STL之set的基本使用
- 【STL】set map的基本用法
- 【STL】map和set的基本用法
- STL之set的基本用法
- C++ STL set 的基本用法
- STL set基本用法
- STL应用——神奇的set
- STL——set的应用
- STL 应用之set
- Fragment Hide() Show()切换实例
- RibbonControl 转 XML树
- dijkstra与spfa(bellman-Ford)
- 编程基础设计与算法-24种设计模式和7大原则
- SpringMVC小结
- STL里set函数的基本应用
- 关于Double、Float精度修改问题
- websocket的小记
- XGboost文献学习笔记
- JavaEE的过往EJB、JNDI、Servlet、JSP、JMS、JTA等规范
- win7远程桌面如何退出全屏或全屏切换
- Bootstrap 字体图标和下拉菜单、按钮
- ctfIDF实验室Fuck your brain
- automake,autoconf使用详解