STL_set用法
2017-11-09 23:58
417 查看
一、set容器简介:
set中所有的元素都会根据元素的键值自动被排序。set的元素不像map那样可以同时拥有key和value,set中元素的键值只有Key。set中不允许两个元素有相同的键值。
二、constructor:
共有三种构造方式:
(1)空容器构造函数(默认构造函数):构造一个空容器,没有元素。
(2)范围构造函数:构造一个具有范围[first,last]的元素的容器,每个元素都由该范围内的相应元素构成。
(3)复制构造函数: 构造一个包含x中每个元素的副本的容器。
注:函数原型中有const key_compare& comp = key_compare()为提供缺省值的比较大小的仿函数,对于一些类型,我们需要自己专门自己写一个对应的比较大小的仿函数。函数原型如下:
示例:
struct classcomp { bool operator() (const int& left, const int& right) const { return left<right; } } void Test1() { //构造一个空的set set<int> first; //给定左右范围构造 int arr[] = { 1,2,3,4,5 }; set<int> second(arr, arr + 5); //利用迭代器构造 set<int> third(second.begin(), second.end()); //拷贝构造 set<int> fifth(third); //使用给定比较的方式 set<int, classcomp> sixth; }
三、Insert:
分为三种插入方式:
(1)参数仅有要插入的数值,返回值类型为一个pair,可通过pair中的second来判断是否插入成功;
(2)在给定迭代器位置插入数值;
(3)给定范围内插入值;
函数原型如下:
<
b1d6
h4 id="示例-1">示例:
void Test1() { set<int> myset; set<int>::iterator it; pair<set<int>::iterator, bool> ret; //插入一些整数值: for (int i = 1; i <= 5; ++i) myset.insert(i * 10); //插入 10 20 30 40 50 /*1...pair<iterator,bool> insert (const value_type& val);*/ ret = myset.insert(20); // 此时set已存在20,所以应为插入失败,first指向已存在的20 if (ret.second == false) it = ret.first; /*2...iterator insert (iterator position, const value_type& val);*/ myset.insert(it, 25); myset.insert(it, 24); myset.insert(it, 26); //在20之后插入25,24,26 /*3...void insert (InputIterator first, InputIterator last);*/ int myints[] = { 5,10,15 }; myset.insert(myints, myints + 3); for (it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; cout << endl; }
输出结果:
5 10 15 20 24 25 26 30 40 50
四、Erase:
分为三种插入方式:
(1)删除给定迭代器位置的元素;
(2)给定删除的元素数值,进行删除
(3)删除给定范围内的元素;
函数原型如下:
示例:
void Test1() { set<int> myset; set<int>::iterator it; for (int i = 1; i<10; i++) myset.insert(i * 10); // 先插入一些元素为10 20 30 40 50 60 70 80 90 it = myset.begin(); ++it; // it此时指向20 //1...删除给定迭代器位置的元素 myset.erase(it); //2...给删除元素的值 myset.erase(40); //3...删除一个范围内(注意左闭右开) it = myset.find(60); myset.erase(it, myset.end()); for (it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; cout << endl; }
输出结果:
10 30 50
五、Find:
函数原型为:iterator find (const value_type& val) const;给定要查找元素的值,若查找到则返回该元素的迭代器。
示例:
void Test1() { set<int> myset; set<int>::iterator it; for (int i = 1; i <= 5; i++) myset.insert(i * 10); //插入10 20 30 40 50 it = myset.find(20); myset.erase(it); for (it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; cout <<endl; }
输出结果:
####10 30 40 50注:更详细的接口使用方法参考http://www.cplusplus.com/reference/set/set/?kw=set
相关文章推荐
- STL中Set的用法(详+转)
- STL --> set用法
- STL之Set:Set的基本用法
- set的用法 STL
- STL几个(set map vector string)用法
- 【原创】STL部分常用数据结构用法汇总 -优先队列,set
- STL中的常用的vector,map,set,Sort用法
- STL学习:STL库vector、string、set、map用法
- STL常见用法整理_set
- stl_map,set 用法
- 标准模板库 STL-1 编程题#1(Coursera 程序设计与算法 专项课程3;用法:迭代器 ostream_iterator,容器 set)
- STL中的常用的vector,map,set,Sort用法
- stl几个(set map vector string)用法
- STL中的常用的vector,map,set,Sort用法
- STL库set的用法
- STL中的常用的vector,map,set,Sort用法
- C++ 标准模板库STL中set用法介绍
- C++ STL set集合容器常用用法
- C++ STL中set的一些用法
- STL-set-用法