C++标准模板库(STL)——set常见用法详解

  • set的定义  
set<typename> name;

  typename可以是任何基本类型,如int、double、char、结构体等,也可以是STL标准容器,如vector、set、queue等。

set<int> name;
set<double> name;
set<char> name;
set<node> name;    //node是结构体类型 
  • set容器内元素的访问

  set只能通过迭代器访问:

set<typename>::iterator it;

  例如下面的代码:

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(2);
 8     st.insert(5);
 9     st.insert(2);
10     st.insert(6);
11     st.insert(2);
12     for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
13         cout<<*it<<" ";
14     }
15     return 0;
16 }
输出结果:
2 5 6

  从输出结果我们能发现,set内的元素自动递增排序,并且自动去除了重复元素

  • set常用函数

  (1)insert()

  insert(x)可以将x插入到set容器中,自动递增排序并去重,时间复杂度为O(logN),其中N为set内的元素个数,前面的例子即说明了这一点。

  (2)find()

  find(v)返回set中对应的v的迭代器,时间复杂度为O(logN),其中N为set内的元素个数。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     for(int i=0;i<5;i++){
 8         st.insert(i+1);
 9     }
10     set<int>::iterator it=st.find(1);
11     cout<<*it;
12     return 0;
13 }
输出结果:
1

  (3)erase()

  erase()可以删除单个元素,也可以删除一个区间内的所有元素。

  ① 删除单个元素

  st.erase(it),it为所需要删除元素的迭代器。时间复杂度为O(1)。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(100);
 8     st.insert(200);
 9     st.insert(200);
10     st.insert(300);
11     st.erase(st.find(200));    //利用find()函数找到200,然后用erase删除它 
12     for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
13         cout<<*it<<" ";
14     }
15     return 0;
16 }
输出结果:
100 300

  st.erase(v),v为所需要删除元素的值。时间复杂度为O(logN),其中N为set内的元素个数。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(100);
 8     st.insert(200);
 9     st.insert(200);
10     st.insert(300);
11     st.erase(200);    //删除set中值为200的元素 
12     for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
13         cout<<*it<<" ";
14     }
15     return 0;
16 }
输出结果:
100 300

  ② 删除一个区间内的所有元素

  st.erase(first, last)可以删除一个区间内的所有元素,其中first为所需要删除区间的起始迭代器,而last则为所需要删除区间的末尾迭代器的下一个地址,即删除[first,last)。时间复杂度为O(last-first)。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(100);
 8     st.insert(200);
 9     st.insert(300);
10     st.insert(400);
11     set<int>::iterator it=st.find(200);
12     st.erase(it,st.end());    //删除元素200至set末尾之间的元素 
13     for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
14         cout<<*it<<" ";
15     }
16     return 0;
17 }
输出结果:
100

  (4)size()

  size()用来获得set内元素的个数,时间复杂度为O(1)。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(1);
 8     st.insert(2);
 9     st.insert(3);
10     st.insert(4);
11     cout<<st.size(); 
12     return 0;
13 }
输出结果:
4

  (5)clear()

  clear()用来清空set中的所有元素,时间复杂度O(N),其中N为set内的元素个数。

 1 #include <iostream> 
 2 #include <set>
 3 using namespace std;
 4 set<int> st;
 5 int main()
 6 {
 7     st.insert(1);
 8     st.insert(2);
 9     st.insert(3);
10     st.insert(4);
11     st.clear();        //清空set 
12     cout<<st.size(); 
13     return 0;
14 }
输出结果:
0

 

posted @ 2021-05-27 17:56  熊猫耳朵  阅读(635)  评论(0编辑  收藏  举报