set常用接口及模拟实现
目录
1>容器
a.序列式容器
b.关联式容器
2>set的使用
a.概念
b.构造和迭代器
c.增删查
d.insert和迭代器
e.find和erase
f.multiset和set的差异
g.Leecode
① 环形链表
1>容器
本篇文章介绍的set底层是红⿊树,红⿊树是⼀颗平衡⼆叉搜索树。set是key搜索场景的结构
a.序列式容器
STL中的部分容器如:string、vector、list、deque、array、forward_list等,这些容器统称为序列式容器,因为逻辑结构为线性序列的数据结构,两个位置存储的值之间⼀般没有紧密的关联关系,⽐如交换⼀下,它依旧是序列式容器。顺序容器中的元素是按他们在容器中的存储位置来顺序保存和访问的
b.关联式容器
关联式容器也是⽤来存储数据的,与序列式容器不同的是,关联式容器逻辑结构通常是⾮线性结构,两个位置有紧密的关联关系,交换⼀下,它的存储结构就被破坏了。顺序容器中的元素是按关键字来保存和访问的。关联式容器有map/set系列和unordered_map/unordered_set系列
2>set的使用
set和multiset参考⽂档
<set> - C++ Reference
a.概念
① set的声明如下,T就是set底层关键字的类型
② set默认要求T⽀持⼩于⽐较,如果不⽀持或者想按⾃⼰的需求⾛可以⾃⾏实现仿函数传给第⼆个模版参数
③ set底层存储数据的内存是从空间配置器申请的,如果需要可以⾃⼰实现内存池,传给第三个参数
④ ⼀般情况下,我们都不需要传后两个模版参数
⑤ set底层是⽤红⿊树实现,增删查效率是O(logN) ,迭代器遍历是⾛的搜索树的中序,所以是有序的
// 底层
template < class T, // set::key_type/value_typeclass Compare = less<T>, // set::key_compare/value_compareclass Alloc = allocator<T> // set::allocator_type> class set;
b.构造和迭代器
set的⽀持正向和反向迭代遍历,遍历默认按升序顺序,因为底层是⼆叉搜索树,迭代器遍历⾛的中序;⽀持迭代器就意味着⽀持范围for,set的iterator和const_iterator都不⽀持迭代器修改数据,修改关键字数据,破坏了底层搜索树的结构
// empty (1) ⽆参默认构造
explicit set(const key_compare& comp = key_compare(),const allocator_type& alloc = allocator_type());// range (2) 迭代器区间构造
template <class InputIterator>
set(InputIterator first, InputIterator last,const key_compare& comp = key_compare(),const allocator_type & = allocator_type());// copy (3) 拷⻉构造
set(const set& x);
// initializer list (5) initializer 列表构造
set(initializer_list<value_type> il,const key_compare& comp = key_compare(),const allocator_type& alloc = allocator_type());// 迭代器是⼀个双向迭代器
iterator->a bidirectional iterator to const value_type// 正向迭代器
iterator begin();
iterator end();// 反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();
c.增删查
Member types
key_type->The first template parameter(T)
value_type->The first template parameter(T)// 单个数据插⼊,如果已经存在则插⼊失败
pair<iterator, bool> insert(const value_type& val);// 列表插⼊,已经在容器中存在的值不会插⼊
void insert(initializer_list<value_type> il);// 迭代器区间插⼊,已经在容器中存在的值不会插⼊
template <class InputIterator>
void insert(InputIterator first, InputIterator last);// 查找val,返回val所在的迭代器,没有找到返回end()
iterator find(const value_type& val);// 查找val,返回Val的个数
size_type count(const value_type& val) const;// 删除⼀个迭代器位置的值
iterator erase(const_iterator position);// 删除val,val不存在返回0,存在返回1
size_type erase(const value_type& val);// 删除⼀段迭代器区间的值
iterator erase(const_iterator first, const_iterator last);// 返回⼤于等val位置的迭代器
iterator lower_bound(const value_type& val) const;// 返回⼤于val位置的迭代器
iterator upper_bound(const value_type& val) const;
d.insert和迭代器
void test_set1()
{// 去重+升序排序set<int> s;// 去重+降序排序(给⼀个⼤于的仿函数)//set<int, greater<int>> s;s.insert(5);s.insert(2);s.insert(7);s.insert(5);//set<int>::iterator it = s.begin();auto it = s.begin();while (it != s.end()){// error C3892: “it”: 不能给常量赋值// *it = 1;cout << *it << " ";++it;}cout << endl;// 插⼊⼀段initializer_list列表值,已经存在的值插⼊失败s.insert({ 2,8,3,9 });for (auto e : s){cout << e << " ";} cout << endl;set<string> strset = { "sort", "insert", "add" };// 遍历string⽐较ascll码⼤⼩顺序遍历的for (auto& e : strset){cout << e << " ";} cout << endl;
}
e.find和erase
void test_set2()
{set<int> s = { 4,2,7,2,8,5,9 };for (auto e : s){cout << e << " ";} cout << endl;// 删除最⼩值s.erase(s.begin());cout << "删除最小值: ";for (auto e : s){cout << e << " ";}cout << endl;// 直接删除xint x;cin >> x;int num = s.erase(x);cout << "直接删除x: ";if (num == 0){cout << x << "不存在!" << endl;} for (auto e : s){cout << e << " ";} cout << endl;// 直接查找在利⽤迭代器删除xcin >> x;cout << "直接查找在利用迭代器删除x: ";auto pos = s.find(x);if (pos != s.end()){s.erase(pos);} else{cout << x << "不存在! ";} for (auto e : s){cout << e << " ";} cout << endl;// 算法库的查找 O(N)auto pos1 = find(s.begin(), s.end(), x);// set⾃⾝实现的查找 O(logN)auto pos2 = s.find(x);// 利⽤count间接实现快速查找cin >> x;cout << "利用count间接实现快速查找: ";if (s.count(x)){cout << x << "在!" << endl;}else{cout << x << "不存在!" << endl;} return 0;
}
void test_set3()
{std::set<int> myset;for (int i = 1; i < 10; i++)myset.insert(i * 10); for (auto e : myset){cout << e << " ";} cout << endl;// 实现查找到的[itlow,itup)包含[30, 60]区间// 返回 >= 30auto itlow = myset.lower_bound(30);// 返回 > 60auto itup = myset.upper_bound(60);// 删除这段区间的值myset.erase(itlow, itup);for (auto e : myset){cout << e << " ";} cout << endl;return 0;
}
f.multiset和set的差异
multiset和set的使⽤基本完全类似,主要区别点在于multiset⽀持值冗余
void test_multiset()
{// 相⽐set不同的是,multiset是排序,但是不去重multiset<int> s = { 2,6,7,8,6,2,8,5,6,9,6,8 };auto it = s.begin();while (it != s.end()){cout << *it << " ";++it;} cout << endl;// 相⽐set不同的是,x可能会存在多个,find查找中序的第⼀个int x;cin >> x;auto pos = s.find(x);cout << "查找:";while (pos != s.end() && *pos == x){cout << *pos << " ";++pos;} cout << endl;// 相⽐set不同的是,count会返回x的实际个数cout << "个数:" << s.count(x) << endl;// 相⽐set不同的是,erase给值时会删除所有的xs.erase(x);cout << "删除:";for (auto e : s){cout << e << " ";} cout << endl;return 0;
}
g.Leecode
① 环形链表
142. 环形链表 II - 力扣(LeetCode)
这道题我在初阶数据结构里的单链表OJ题这篇文章介绍过,当时我是用C语言实现的,代码相对来说复杂一些,如果使用现在介绍的set解这道题会简单很多
class Solution {
public:ListNode *detectCycle(ListNode *head) {set<ListNode*> s;ListNode* pcur = head;while(pcur){auto ret = s.insert(pcur);if(ret.second == false)return pcur;pcur = pcur->next;}return NULL;}
};
这个insert接口的返回值设计的很妙,它的bool就代表这个对象是否存在该容器里,所以这里使用set去解这道题非常的香!
本篇文章到这里就结束啦,希望这些内容对大家有所帮助!
下篇文章见,希望大家多多来支持一下!
感谢大家的三连支持!