標準模板庫(STL)
1. 六大組件
容器(Containers):
- 用于存儲和管理數據集合的數據結構,如
vector、list、map等。 - 提供了數據的組織、訪問和修改的方法。
迭代器(Iterators):
- 用于遍歷容器中的元素。
- 迭代器是一個類,它封裝了指針,并提供類似指針的行為。
算法(Algorithms):
- 提供了一系列通用的算法,如排序、搜索、變換等。
- 算法通常接受迭代器作為參數,使其可以操作各種容器。
仿函數(Functors):
- 是可以像函數一樣使用的類的實例。
- 它們通常重載了函數調用操作符
operator()。
適配器(Adapters):
- 用于修改容器、迭代器或函數的行為。
- 包括迭代器適配器、函數適配器等。
空間配置器(Allocators):
- 用于管理動態內存的分配和釋放。
- 控制容器內部如何分配和釋放內存。
2. 容器及使用
容器是STL中用于存儲數據的組件,它們提供了一系列的模板類,用于存儲和管理數據集合。容器可以分為序列容器(如vector、deque、list、array)、關聯容器(如set、map)、容器適配器(如stack、queue、priority_queue)等。
- 用法:容器通常提供構造函數、迭代器、成員函數(如
size()、empty()、push_back()等)來管理數據。
3. 迭代器
迭代器是STL中用于遍歷容器元素的工具。迭代器可以指向容器中的某個元素,支持前進、后退、元素訪問等操作。
- 用法:通過調用容器的
begin()和end()成員函數獲取迭代器,然后使用循環結構遍歷容器。
4. 空間配置器及其使用
空間配置器負責容器內部的內存分配和釋放。STL提供了默認的空間配置器allocator,也可以自定義空間配置器。
- 用法:通過調用空間配置器的
allocate()和deallocate()成員函數來分配和釋放內存。容器構造時可以指定使用的空間配置器。
5. 算法
STL提供了大量的算法,這些算法可以對容器中的元素進行操作,如排序、搜索、變換等。
- 用法:直接調用STL算法,并將容器的迭代器作為參數傳遞。例如,使用
sort()算法對vector進行排序。
6. 仿函數
仿函數是一類特殊的對象,它們重載了函數調用操作符,可以像函數一樣被調用。
- 用法:創建仿函數對象,并像調用函數一樣使用它們。例如,使用
plus<int>()來計算兩個整數的和。
7. 適配器
適配器是一種修改已有組件行為的工具,包括迭代器適配器、函數適配器等。
- 用法:使用適配器來改變迭代器的行為,或者將函數或仿函數轉換成另一種形式。例如,使用
bind來綁定函數的某些參數,或者使用mem_fn來調用類的成員函數。
容器實現
由于要求每個容器都展示多種操作,我會為每個容器編寫一個示例代碼,展示它們的常用操作。為了簡化代碼,我將在代碼中省略std::前綴,并在開頭聲明命名空間using namespace std;。
1. 序列式容器
vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// 插入
vec.insert(vec.begin() + 2, 6); // 在索引2的位置插入6
// 刪除
vec.erase(vec.begin() + 2); // 刪除索引2的元素
// 修改
vec[2] = 10; // 修改索引2的元素為10
// 判斷是否為空
bool isEmpty = vec.empty() ? true : false;
// 查找
auto it = find(vec.begin(), vec.end(), 4);
// 排序
sort(vec.begin(), vec.end());
// 逆序
reverse(vec.begin(), vec.end());
// 清空
clear(vec);
// 迭代器操作
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
deque
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> deq = {1, 2, 3, 4, 5};
// 插入
deq.push_back(6);
deq.push_front(0);
// 刪除
deq.pop_back();
deq.pop_front();
// 修改
deq[2] = 10;
// 判斷是否為空
bool isEmpty = deq.empty() ? true : false;
// 查找
it = find(deq.begin(), deq.end(), 4);
// 迭代器操作
for (auto it = deq.rbegin(); it != deq.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
list
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> lst = {1, 2, 3, 4, 5};
// 插入
lst.insert(lst.begin(), 0);
lst.insert(++lst.begin(), 6);
// 刪除
lst.erase(++lst.begin()); // 刪除第二個元素
// 修改
*(++lst.begin()) = 10; // 修改第二個元素為10
// 判斷是否為空
bool isEmpty = lst.empty() ? true : false;
// 查找
auto it = find(lst.begin(), lst.end(), 4);
// 迭代器操作
for (auto it = lst.rbegin(); it != lst.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
2. 關聯式容器
set
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> s = {1, 2, 3, 4, 5};
// 插入
s.insert(6);
// 刪除
s.erase(3);
// 修改
// Set不提供直接修改元素的接口,需要刪除后重新插入
// 判斷是否為空
bool isEmpty = s.empty() ? true : false;
// 查找
auto it = s.find(4);
// 迭代器操作
for (auto it = s.rbegin(); it != s.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
multiset
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> ms = {1, 2, 2, 3, 4};
// 插入
ms.insert(5);
// 刪除
ms.erase(ms.find(2)); // 刪除一個2
// 判斷是否為空
bool isEmpty = ms.empty() ? true : false;
// 查找
auto it = find(ms.begin(), ms.end(), 4);
// 迭代器操作
for (auto it = ms.rbegin(); it != ms.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
map
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> mp = {{1, "one"}, {2, "two"}, {3, "three"}};
// 插入
mp.insert({4, "four"});
// 刪除
mp.erase(2);
// 修改
mp[3] = "three modified";
// 判斷是否為空
bool isEmpty = mp.empty() ? true : false;
// 查找
auto it = mp.find(3);
// 迭代器操作
for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
cout << it->first << ": " << it->second << " ";
}
cout << endl;
return 0;
}
multimap
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int, string> mmp = {{1, "one"}, {2, "two"}, {2, "two again"}};
// 插入
mmp.insert({3, "three"});
// 刪除
mmp.erase(2); // 刪除所有鍵為2的元素
// 判斷是否為空
bool isEmpty = mmp.empty() ? true : false;
// 查找
auto range = mmp.equal_range(2);
// 迭代器操作
for (auto it = range.first; it != range.second; ++it) {
cout << it->first << ": " << it->second << " ";
}
cout << endl;
return 0;
}
請注意,這些示例代碼展示了每個容器的基本操作,包括插入、刪除、修改(如果支持)、查找、判斷是否為空、迭代器操作和打印。由于set和map是有序容器,它們的元素會自動排序。multiset和multimap允許重復的元素。在實際使用中,你可能需要根據具體需求調整這些操作。
浙公網安備 33010602011771號