C++ STL(规范模板库)提供了一组丰盛的容器和算法,使得开发者可能更加高效地编写程序。本文将介绍STL中的一些罕用容器和算法。
容器
vector
vector
是一个动静数组,能够在运行时调整大小。它的长处在于能够疾速地拜访元素,毛病是在插入和删除元素时须要挪动前面的元素。
#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
还提供了很多其余的成员函数和迭代器,能够不便地拜访和批改元素。比方,能够应用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
也提供了很多其余的成员函数和迭代器,能够不便地拜访和批改元素。比方,能够应用l.front()
和l.back()
别离拜访首元素和尾元素,应用l.insert()
和l.erase()
在任意地位插入和删除元素。此外,list
还提供了l.empty()
和l.size()
别离判断容器是否为空和获取容器大小。
map
map
是一个键值对容器,能够疾速地依据键值查找对应的值。
#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
也提供了很多其余的成员函数和迭代器,能够不便地拜访和批改元素。比方,能够应用m.find()
查找元素,应用m.insert()
插入元素,应用m.erase()
删除元素。此外,map
还提供了m.empty()
和m.size()
别离判断容器是否为空和获取容器大小。
算法
除了容器,STL还提供了一些罕用的算法,能够不便地操作容器中的元素。
sort
sort
是一个排序算法,能够疾速地将数组或容器中的元素依照指定规定排序。
#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
的第一个参数是要排序的容器的起始地址,第二个参数是要排序的容器的完结地址。这里应用了vector
的begin()
和end()
函数获取迭代器,也能够应用数组名和数组长度作为参数。
sort
默认是升序排序,能够通过第三个参数指定排序规定。比方,能够应用greater<int>()
降序排序,应用less<int>()
升序排序。
find
find
是一个查找算法,能够疾速地在数组或容器中查找指定元素。
#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
的第一个参数是要查找的容器的起始地址,第二个参数是要查找的容器的完结地址,第三个参数是要查找的元素。find
返回一个迭代器,指向第一个等于要查找元素的地位,如果没有找到则返回容器的完结地址。
除了find
,STL还提供了很多其余的查找算法,比方find_if
能够依据指定规定查找元素,binary_search
能够判断容器中是否含有指定元素,lower_bound
和upper_bound
能够查找元素的下界和上界。
论断
本文介绍了C++ STL中的一些罕用容器和算法,它们能够大大提高开发效率,开发者应该熟练掌握它们的应用。除了本文介绍的容器和算法,STL还提供了很多其余的容器和算法,能够依据具体的需要抉择应用。在应用STL时,要留神容器和算法的复杂度,避免出现性能问题。