一、set容器介紹
Set是C++ STL的一種關聯式容器,它類似於一個集合,其中不可以出現相同的元素,同時元素是按照一定順序排列的。
Set容器是通過對元素進行排序和去重來實現這些特性的,因此在使用時需要注意元素的比較和去重規則。
Set的內部實現是紅黑樹,因此插入、刪除和查找元素操作都具有較好的時間複雜度,常數級別的低。
二、set容器的基本操作
對於Set容器,最基本的操作包括插入元素、刪除元素和查找元素。下面是對這三個操作的介紹:
1. 插入元素
#include <set>
#include <iostream>
int main() {
std::set s;
s.insert(3);
s.insert(1);
s.insert(4);
s.insert(1);
for (auto it = s.begin(); it != s.end(); it++) {
std::cout << *it << " ";
}
return 0;
}
輸出結果為:1 3 4。
從結果可以看到,1並沒有被重複插入,這是因為Set容器的特性,它會自動去重。
2. 刪除元素
#include <set>
#include <iostream>
int main() {
std::set s;
s.insert(3);
s.insert(1);
s.insert(4);
s.erase(1);
for (auto it = s.begin(); it != s.end(); it++) {
std::cout << *it << " ";
}
return 0;
}
輸出結果為:3 4。
從結果可以看到,我們通過erase函數刪除了Set容器中的一個元素1,Set容器的自動去重特性也保證了刪除操作的正確性。
3. 查找元素
#include <set>
#include <iostream>
int main() {
std::set s;
s.insert(3);
s.insert(1);
s.insert(4);
auto it = s.find(3);
if (it != s.end()) {
std::cout << "Found " << *it << std::endl;
} else {
std::cout << "Not Found" << std::endl;
}
return 0;
}
輸出結果為:Found 3。
從結果可以看到,我們通過find函數在Set容器中查找了元素3,並輸出了查找結果。
三、set容器的排序規則
Set容器的特性使得其中的元素按照一定規則排序,常見的排序規則包括從小到大排序和從大到小排序。
在C++ STL的Set容器中,默認的排序規則是從小到大排序,即元素類型的小於號(<)被定義為元素的比較運算符。如果需要修改Set容器的排序規則,可以通過自定義比較函數來實現。
1. 從小到大排序
#include <set>
#include <iostream>
struct Student {
std::string name;
int age;
bool operator<(const Student& other) const {
return name < other.name;
}
};
int main() {
std::set s;
s.insert({"Tom", 18});
s.insert({"Alice", 20});
s.insert({"Bob", 19});
for (auto& it : s) {
std::cout << it.name << " " << it.age << std::endl;
}
return 0;
}
輸出結果為:Alice 20
Bob 19
Tom 18
從結果可以看出,這裡我們自定義了Student結構體的小於號比較運算符為按照姓名從小到大排序,因此Set容器中的元素被按照這個規則排序了。
2. 從大到小排序
#include <set>
#include <iostream>
struct Student {
std::string name;
int age;
bool operator other.name;
}
};
int main() {
std::set s;
s.insert({"Tom", 18});
s.insert({"Alice", 20});
s.insert({"Bob", 19});
for (auto& it : s) {
std::cout << it.name << " " << it.age << std::endl;
}
return 0;
}
輸出結果為:Tom 18
Bob 19
Alice 20
從結果可以看出,這裡我們同樣自定義了Student結構體的小於號比較運算符,不同的是按照姓名從大到小排序。這個比較函數和從小到大排序的比較函數僅僅是在小於號的返回值上取反了而已。
四、set容器的應用場景
Set容器作為一個可以去重排序的容器,在實際開發中有許多應用場景,下面介紹其中的兩個常見的應用場景。
1. 統計單詞數目
#include <set>
#include <iostream>
#include <sstream>
int main() {
std::string str = "apple banana apple orange banana";
std::istringstream iss(str);
std::set s;
std::string word;
while (iss >> word) {
s.insert(word);
}
std::cout << s.size() << std::endl;
return 0;
}
輸出結果為:3。
這裡我們利用了Set容器的自動去重特性來統計字元串中單詞數目。將字元串中的單詞插入到Set容器中,這樣Set容器中的元素就是不重複的單詞,Set容器的size()函數就可以得到單詞數目了。
2. 求兩個數組的交集
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector v1 = {1, 3, 5, 7, 9};
std::vector v2 = {2, 3, 5, 7, 8};
std::set s1(v1.begin(), v1.end());
std::set s2(v2.begin(), v2.end());
std::vector v3;
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(v3));
for (auto& it : v3) {
std::cout << it << " ";
}
return 0;
}
輸出結果為:3 5 7。
這裡我們利用了Set容器的自動去重和排序特性,將兩個數組分別轉換為Set容器,這樣兩個Set容器的交集就是兩個數組的交集,利用STL的set_intersection演算法,便可以得到兩個數組的交集。
總結
本文對Set容器進行了一系列的介紹和講解,從基本操作到排序規則再到實際應用場景,都有詳細的闡述。Set容器是C++ STL中十分常用且實用的容器,掌握Set容器的使用方法和特性對於編程開發人員來說是很重要的。
原創文章,作者:MFNTA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/332768.html