C++ STL容器和算法:詳解和實例演示

C++ STL(標準模板庫)提供了一組豐富的容器和算法,使得開發(fā)者能夠更加高效地編寫程序。本文將介紹STL中的一些常用容器和算法。
容器
vector
vector
是一個動態(tài)數(shù)組,可以在運行時調(diào)整大小。它的優(yōu)點在于可以快速地訪問元素,缺點是在插入和刪除元素時需要移動后面的元素。
#include <vector>
#include <iostream>
using namespace std;
int main() {
? vector<int> v;
? v.push_back(1);
? v.push_back(2);
? v.push_back(3);
? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;
? v.pop_back();
? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;
? return 0;
}
除了push_back
和pop_back
,vector
還提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用v.front()
和v.back()
分別訪問首元素和尾元素,使用v.insert()
和v.erase()
在任意位置插入和刪除元素。此外,vector
還提供了v.empty()
和v.size()
分別判斷容器是否為空和獲取容器大小。
list
list
是一個雙向鏈表,可以在任意位置插入和刪除元素,但訪問元素比較慢。
#include <list>
#include <iostream>
using namespace std;
int main() {
? list<int> l;
? l.push_back(1);
? l.push_back(2);
? l.push_back(3);
? for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
? ? ? cout << *it << " ";
? }
? cout << endl;
? l.pop_back();
? for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
? ? ? cout << *it << " ";
? }
? cout << endl;
? return 0;
}
和vector
一樣,list
也提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用l.front()
和l.back()
分別訪問首元素和尾元素,使用l.insert()
和l.erase()
在任意位置插入和刪除元素。此外,list
還提供了l.empty()
和l.size()
分別判斷容器是否為空和獲取容器大小。
map
map
是一個鍵值對容器,可以快速地根據(jù)鍵值查找對應(yīng)的值。
#include <map>
#include <iostream>
using namespace std;
int main() {
? map<string, int> m;
? m["a"] = 1;
? m["b"] = 2;
? m["c"] = 3;
? for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
? ? ? cout << it->first << ":" << it->second << " ";
? }
? cout << endl;
? m.erase("b");
? for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
? ? ? cout << it->first << ":" << it->second << " ";
? }
? cout << endl;
? return 0;
}
和vector
、list
一樣,map
也提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用m.find()
查找元素,使用m.insert()
插入元素,使用m.erase()
刪除元素。此外,map
還提供了m.empty()
和m.size()
分別判斷容器是否為空和獲取容器大小。
算法
除了容器,STL還提供了一些常用的算法,可以方便地操作容器中的元素。
sort
sort
是一個排序算法,可以快速地將數(shù)組或容器中的元素按照指定規(guī)則排序。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
? vector<int> v;
? v.push_back(3);
? v.push_back(2);
? v.push_back(1);
? sort(v.begin(), v.end());
? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;
? return 0;
}
sort
的第一個參數(shù)是要排序的容器的起始地址,第二個參數(shù)是要排序的容器的結(jié)束地址。這里使用了vector
的begin()
和end()
函數(shù)獲取迭代器,也可以使用數(shù)組名和數(shù)組長度作為參數(shù)。
sort
默認是升序排序,可以通過第三個參數(shù)指定排序規(guī)則。比如,可以使用greater<int>()
降序排序,使用less<int>()
升序排序。
find
find
是一個查找算法,可以快速地在數(shù)組或容器中查找指定元素。
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
? vector<int> v;
? v.push_back(1);
? v.push_back(2);
? v.push_back(3);
? vector<int>::iterator it = find(v.begin(), v.end(), 2);
? if (it != v.end()) {
? ? ? cout << "found " << *it << endl;
? } else {
? ? ? cout << "not found" << endl;
? }
? return 0;
}
find
的第一個參數(shù)是要查找的容器的起始地址,第二個參數(shù)是要查找的容器的結(jié)束地址,第三個參數(shù)是要查找的元素。find
返回一個迭代器,指向第一個等于要查找元素的位置,如果沒有找到則返回容器的結(jié)束地址。
除了find
,STL還提供了很多其他的查找算法,比如find_if
可以根據(jù)指定規(guī)則查找元素,binary_search
可以判斷容器中是否含有指定元素,lower_bound
和upper_bound
可以查找元素的下界和上界。
結(jié)論
本文介紹了C++ STL中的一些常用容器和算法,它們可以大大提高開發(fā)效率,開發(fā)者應(yīng)該熟練掌握它們的使用。除了本文介紹的容器和算法,STL還提供了很多其他的容器和算法,可以根據(jù)具體的需求選擇使用。在使用STL時,要注意容器和算法的復雜度,避免出現(xiàn)性能問題。