STL set相关学习笔记
来源:互联网 发布:数据挖掘需要什么基础 编辑:程序博客网 时间:2024/05/14 20:23
#include <iostream>using namespace std;#include<set>/********************************************************************************bool fncomp (int lhs, int rhs){ return lhs<rhs;}class classcomp{ public: bool operator() (const int& lhs, const int& rhs) const { return lhs>rhs; }};int main(){ //set不支持:operator[] //set构造函数:// 1.用数组初始化:如下 int myints[]= {10,10,30,80,50}; set<int> second (myints,myints+5);//初始化的时候会把数组中重复的数字去掉,并自动排好序 set<int>::iterator s=second.begin(); while(s!=second.end()) { cout<<*s<<" "; s++; } //输出:10 30 50 80///////////////////////////////////////////////////////////////// 2.用另一个set对象初始化 set<int> third (second); s=third.begin(); cout<<endl; while(s!=third.end()) { cout<<*s<<" "; s++; } //输出:10 30 50 80//////////////////////////////////////////////////////////////// 3.用一个set对象做初始化 set<int> fourth (second.begin(), --second.end()); s=fourth.begin(); cout<<endl; while(s!=fourth.end()) { cout<<*s<<" "; s++; } //输出:10 30 50//////////////////////////////////////////////////////////////// int arr[]={1,2,3,4,6,6,7,8,5}; set<int,classcomp> fifth(arr,arr+9);//classcomp表示排序方式,用数组arr初始化set,去重并按照classcomp排序规则进行排序。 s=fifth.begin(); cout<<endl; while(s!=fifth.end()) { cout<<*s<<" "; s++; } ////输出:8 7 6 5 4 3 2 1//////////////////////////////////////////////////////////////// bool(*fn_pt)(int,int) = fncomp; set<int,bool(*)(int,int)> sixth (fn_pt);//参数必须作为函数指针。 sixth.insert(fifth.begin(),fifth.end()); s=sixth.begin(); cout<<endl; while(s!=sixth.end()) { cout<<*s<<" "; s++; }////////////////////////////////////////////////////////////// return 0;}*********************************************************************************************/int main(){/********************************** //begin()、end()、rbegin()、rend()函数: int a[]={1,2,3,4,5}; set<int> s0(a,a+5); set<int>::iterator it; it=s0.begin(); it++;//不能这么写it+=1或者it=it+1。 //其他类似。************************************/ //clear():清空容器 int a[]={1,2,3,4,5}; set<int> s0(a,a+5); s0.clear();//s0什么也没有了 //count()函数: int a1[]={1,2,3,4,5}; set<int> s1(a1,a1+5); cout<<s1.count(10);//查看10是不是容器中元素,不是返回0. //empty():判断容器是否为空,空的话返回true,非空返回false. bool falg=s1.empty(); //equal_range()函数: pair<set<int>::const_iterator,set<int>::const_iterator>ret; ret=s1.equal_range(4); cout<<" "<<*ret.first;//输出:4 cout<<" "<<*ret.second;//输出:5 return 0;}int main(){ //insert()、erase()、find() set<int> myset; set<int>::iterator it; for (int i=9; i>=1; i--) myset.insert(i*10);//向容器中添加元素,并排序。返回值的类型为pair<iterator,bool>/**************************************************************** set<int> myset; set<int>::iterator it; for (int i=9; i>=1; i--) myset.insert(i*10); pair<std::set<int>::iterator,bool> ret; ret = myset.insert(20);//插入会失败。 cout<<*ret.first<<endl;//输出20 cout<<ret.second<<endl;//输出0 myset.insert (it,25);//不管插入成功与否,都会返回it。*******************************************************************/ it = myset.begin(); ++it; int a0[]={1,2,3,44,5}; set<int>s; s.insert(a0,a0+5);//插入数组中的值,无返回值。 myset.erase (it);//把*it的值从容器中删除,没有返回值 cout<<endl<<myset.erase (60)<<" ";//从容器中找到40删除,返回1,没有这个元素返回0。 //iterator find (const value_type& val) const; it = myset.find (40);//在容器中查找-3这个元素,找到返回iterator位置,找不到返回end()位置。 myset.erase (it, myset.end());//从it开始删除知道end()位置(不删除)结束 return 0;}int main(){/************************************************************************************* //key_comp():原型 key_compare key_comp() const; set<int> myset; int highest; set<int>::reverse_iterator rit;//不能这么判断rit<=myset.rend();只可以==或者!= set<int>::key_compare mycomp = myset.key_comp();//key_comp()好像只能这么定义。 for (int i=0; i<=5; i++) myset.insert(i); set<int>::iterator it=myset.begin();//也不能这么判断it<=myset.end() it++; rit=myset.rbegin(); cout<<mycomp(*it,*it);//判断容器中两个元素的大小,若第一个小于或等于第二个,返回0,否则返回1***************************************************************************************************//*************************************************************** //lower_bound()、upper_bound()作用 //iterator lower_bound (const value_type& val) const; //iterator upper_bound (const value_type& val) const; set<int> myset; set<int>::iterator itlow,itup; for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itlow=myset.lower_bound (30); // 返回容器中第一个值【大于或等于】30的元素的iterator位置。 cout<<*itlow<<endl; itup=myset.upper_bound (60); //返回容器中第一个值【大于】60的元素的iterator位置。 cout<<*itup<<endl;*******************************************************************//************************************************ //operator=、swap()、size(): int myints[]={ 12,82,37,64,15 }; set<int> first (myints,myints+5); set<int> second; second = first;//现在second是first, first = std::set<int>();//现在first为空。 cout<<first.size()<<endl;//输出:0 cout<<second.size()<<endl;//输出:5////////////////////////////////////////////////////// //void swap (set& x); first.swap(second);//交换first与second cout<<first.size()<<endl;//输出:5 cout<<second.size()<<endl;//输出:0**********************************************************///value_comp()函数// value_compare value_comp() const; set<int> myset; set<int>::value_compare mycomp = myset.value_comp(); for (int i=0; i<=5; i++) myset.insert(i); int highest=*myset.rbegin(); set<int>::iterator it=myset.begin(); cout<<highest<<" "<<*it<<endl; cout<<mycomp(highest,*it);//比较两个数的大小,若第一个大于第二个,返回0;否则返回1.(参数只能是容器中的值,否则永远返回1) return 0;}
阅读全文
0 0
- STL set相关学习笔记
- STL set、multiset 学习笔记
- C++ STL set 学习笔记
- C++ STL 学习笔记 set
- STL中的set学习笔记
- 《STL源码剖析》学习笔记-第6章(一) set相关算法
- C++ STL学习笔记七 set容器
- c++ STL set和multiset 学习笔记
- STL学习笔记之容器--set
- C++ STL学习笔记七 set容器
- STL学习笔记-set/multiset容器(集合)
- STL之set相关算法
- stl中set相关算法
- STL学习之set
- STL学习--set
- STL学习:set
- stl学习—set
- STL学习笔记----5.容器 set 和 multiset
- Android 关于极光推送退出APP时清空通知的问题
- Unity UI系统-NGUI简介
- Launching Spark on YARN
- CSS in JS 简介
- tomcat启动没报错,在Myclipse中也没报错,但是网页永远显示404
- STL set相关学习笔记
- 利用python进行数据分析 移动数据系数与线性回归(P366)
- 换位思考的应用场景
- element ui 重写列
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- Mysql必知必会
- Eclipse导入外部项目问题总结1
- Python 协程
- Spring Batch