STL之set常用函數詳解
1.關於set
C++ STL 之所以得到廣泛的讚譽,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封裝了許多複雜的數據結構算法和大量常用數據結構操作。vector封裝數組,list封裝了鏈表,map和set封裝了二叉樹等,在封裝這些數據結構的時候,STL按照程序員的使用習慣,以成員函數方式提供的常用操作,如:插入、排序、刪除、查找等。讓用戶在STL使用過程中,並不會感到陌生。
關於set,必須說明的是set關聯式容器。set作爲一個容器也是用來存儲同一數據類型的數據類型,並且能從一個數據集合中取出數據,在set中每個元素的值都唯一,而且系統能根據元素的值自動進行排序。應該注意的是set中數元素的值不能直接被改變。C++ STL中標準關聯容器set, multiset, map, multimap內部採用的就是一種非常高效的平衡檢索二叉樹:紅黑樹,也成爲RB樹(Red-Black Tree)。RB樹的統計性能要好於一般平衡二叉樹,所以被STL選擇作爲了關聯容器的內部結構。
- 內部自動有序
- 不含重複元素的容器
2.set的定義 必須引入頭文件 #include <set>
- 定義單個集合
set<typename> name;
- set數組的定義和vector相同
set<typename> Arrayname[100] // 這裏定義了一百個set容器,下標爲0~99;
3.set元素的訪問
- set中的元素只能通過迭代器訪問。
set<typename>::iterator it;
這樣就得到了迭代器it,並且可以通過*it來訪問set裏面的元素。
除開vector和string之外的STL都不支持*(it+i)的訪問方式
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<int> a;
int n;
cin>>n; // n代表要輸入的數
while(n--)
{
int tmp;
cin>>tmp;
a.insert(tmp);
}
for(auto it=a.begin();it!=a.end();it++)
cout<<*it<<" ";
return 0;
}
// 輸出結果:
G:\clion\qifei\cmake-build-debug\qifei.exe
6
6
5
4
7
5
4
4 5 6 7
Process finished with exit code 0
4.set常用函數的實例解析
- insert(x),可以將元素x插入set容器中。
- find(x),返回X所對應值的迭代器。
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<int> a;
int n;
cin>>n; // n代表要輸入的數
while(n--)
{
int tmp;
cin>>tmp;
a.insert(tmp);
}
set<int>::iterator it=a.find(5);
cout<<*it<<endl;
return 0;
}
// 輸出結果:
G:\clion\qifei\cmake-build-debug\qifei.exe
3
1
2
5
5
Process finished with exit code 0
-
erase()
-
刪除單個元素
-
a.erase(it),it爲要刪除的元素的迭代器
-
#include <iostream> #include <set> #include <string> using namespace std; int main() { set<int> a; int n; cin>>n; // n代表要輸入的數 while(n--) { int tmp; cin>>tmp; a.insert(tmp); } set<int>::iterator it=a.find(5); a.erase(it); for(it=a.begin();it!=a.end();it++) cout<<*it<<" "; return 0; } // 輸出結果: G:\clion\qifei\cmake-build-debug\qifei.exe 5 4 5 1 2 3 1 2 3 4 Process finished with exit code 0
-
a.erase(value),value爲要刪除的元素的值。
-
#include <iostream> #include <set> #include <string> using namespace std; int main() { set<int> a; int n; cin>>n; // n代表要輸入的數 while(n--) { int tmp; cin>>tmp; a.insert(tmp); } set<int>::iterator it; a.erase(8); for(it=a.begin();it!=a.end();it++) cout<<*it<<" "; return 0; } // 輸出結果: G:\clion\qifei\cmake-build-debug\qifei.exe 4 8 5 3 9 3 5 9 Process finished with exit code 0
-
a.erase(first,last),first爲對應要刪除區間的開始的迭代器,last爲要刪除區間的尾迭代器的下一個地址。
-
#include <iostream> #include <set> #include <string> using namespace std; int main() { set<int> a; int n; cin>>n; // n代表要輸入的數 while(n--) { int tmp; cin>>tmp; a.insert(tmp); } set<int>::iterator it=a.find(8); set<int>::iterator ptr=a.find(3); a.erase(ptr,it); for(it=a.begin();it!=a.end();it++) cout<<*it<<" "; return 0; } // 輸出結果: G:\clion\qifei\cmake-build-debug\qifei.exe 8 1 2 4 3 8 5 6 10 1 2 8 10 Process finished with exit code 0
-
-
size()用來獲得set的元素個數。
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<int> a;
int n;
cin>>n; // n代表要輸入的數
while(n--)
{
int tmp;
cin>>tmp;
a.insert(tmp);
}
cout<<a.size()<<endl;
return 0;
}
// 輸出結果:
G:\clion\qifei\cmake-build-debug\qifei.exe
10
1 2 3 4 5 66 7 8 9 3
9
Process finished with exit code 0
- clear(),清空set所有的元素。
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<int> a;
int n;
cin>>n; // n代表要輸入的數
while(n--)
{
int tmp;
cin>>tmp;
a.insert(tmp);
}
a.clear();
cout<<a.size()<<endl;
return 0;
}
// 輸出結果:
G:\clion\qifei\cmake-build-debug\qifei.exe
10
4 5 6 12 3 789 1 2 3 4
0
Process finished with exit code 0
c++ set與unordered_set區別
-
c++ std中set與unordered_set區別和map與unordered_map區別類似:
-
set基於
紅黑樹
實現,紅黑樹具有自動排序
的功能,因此map內部所有的數據,在任何時候,都是有序的。unordered_set基於哈希表
,數據插入和查找的時間複雜度很低,幾乎是常數時間,而代價是消耗比較多的內存,無自動排序功能
。底層實現上,使用一個下標範圍比較大的數組來存儲元素,形成很多的桶,利用hash函數對key進行映射到不同區域進行保存。
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
unordered_set<int> a;
int n;
cin>>n; // n代表要輸入的數
while(n--)
{
int tmp;
cin>>tmp;
a.insert(tmp);
}
for(auto it=a.begin();it!=a.end();it++)
cout<<*it<<" ";
return 0;
}
// 輸出結果:
G:\clion\qifei\cmake-build-debug\qifei.exe
8
1 2 36 4 5 1 2 10
10 5 1 36 2 4 // (順序依賴於 hash function)
Process finished with exit code 0