概述:
STL(Standard Template Library,标准模板库)是C++标准库的一部分,它提供了大量的模板类和函数,用于执行常见的算法和数据结构操作。STL的主要目的是提供一个统一、高效、可重用的数据结构和算法库,以减少程序员在编写这些基础代码时的重复工作。
优点:
- STL 是 C++的一部分,因此不用额外安装什么,它被内建在你的编译器之内。
- 程序员可以不用思考 STL 具体的实现过程,只要能够熟练使用 STL 就 OK 了。
- STL 具有高可重用性,高性能,高移植性,跨平台的优点。
组成:
| 类别 |
示例 |
描述 |
| 容器(Containers) |
|
|
| 序列容器 |
vector |
动态数组,支持快速随机访问 |
|
deque |
双端队列,支持在两端快速插入和删除 |
|
list |
双向链表,支持在任何位置快速插入和删除 |
| 关联容器 |
set |
有序集合,不允许重复元素 |
|
multiset |
有序集合,允许重复元素 |
|
map |
键值对集合,键唯一,值可重复 |
|
multimap |
键值对集合,键可重复 |
| 容器适配器 |
stack |
后进先出(LIFO)容器 |
|
queue |
先进先出(FIFO)容器 |
|
priority_queue |
优先级队列,元素根据优先级排序 |
| 迭代器(Iterators) |
|
访问容器中元素的通用接口 |
|
begin() |
返回指向容器第一个元素的迭代器 |
|
end() |
返回指向容器尾后位置的迭代器(不指向任何元素) |
| 算法(Algorithms) |
|
|
|
sort() |
对容器中的元素进行排序 |
|
find() |
在容器中查找元素 |
|
copy() |
复制容器中的元素 |
|
… |
其他各种算法,如 remove(), reverse(), binary_search() 等 |
| 函数对象(Function Objects) |
|
类似于函数,但可以作为对象传递和存储 |
|
less<T> |
比较两个对象是否小于(默认用于set, map等) |
|
greater<T> |
比较两个对象是否大于 |
|
自定义函数对象 |
自定义比较或操作函数 |
| 适配器(Adapters) |
|
修改已有接口以适应不同需求 |
|
reverse_iterator |
反向迭代器,用于反向遍历容器 |
|
istream_iterator |
读取输入流中的元素 |
|
ostream_iterator |
将元素写入输出流 |
| 分配器(Allocators) |
|
管理容器中的内存分配 |
|
allocator<T> |
默认分配器,使用new和delete |
|
自定义分配器 |
根据特定需求管理内存 |
常用容器
string容器
构造函数:
1 2 3 4
| string(); string(const string& str); string(const char* s); string(int n, char c);
|
基本赋值:
1 2 3 4 5 6 7 8
| string& operator=(const char* s); string& operator=(const string &s); string& operator=(char c); string& assign(const char *s); string& assign(const char *s, int n); string& assign(const string &s); string& assign(int n, char c); string& assign(const string &s, int start, int n);
|
存取字符:
1 2
| char& operator[](int n); char& at(int n);
|
拼接操作:
1 2 3 4 5 6 7 8
| string& operator+=(const string& str); string& operator+=(const char* str); string& operator+=(const char c); string& append(const char *s); string& append(const char *s, int n); string& append(const string &s); string& append(const string &s, int pos, int n); string& append(int n, char c);
|
查找替换:
1 2 3 4 5 6 7 8 9 10
| int find(const string& str, int pos = 0) const; int find(const char* s, int pos = 0) const; int find(const char* s, int pos, int n) const; int find(const char c, int pos = 0) const; int rfind(const string& str, int pos = npos) const; int rfind(const char* s, int pos = npos) const; int rfind(const char* s, int pos, int n) const; int rfind(const char c, int pos = 0) const; string& replace(int pos, int n, const string& str); string& replace(int pos, int n, const char* s);
|
比较操作:
1 2 3 4 5 6 7 8
|
int compare(const string &s) const; int compare(const char *s) const;
|
取出子串:
1
| string substr(int pos = 0, int n = npos) const;
|
插入删除:
1 2 3 4
| string& insert(int pos, const char* s); string& insert(int pos, const string& str); string& insert(int pos, int n, char c); string& erase(int pos, int n = npos);
|
c-style转换:
1 2 3 4 5 6
| string str = "it"; const char* cstr = str.c_str();
char* s = "it"; string str(s);
|
vector容器
构造函数
1 2 3 4
| vector<T> v; vector(v.begin(), v.end()); vector(n, elem); vector(const vector &vec);
|
赋值操作
1 2 3 4
| assign(beg, end); assign(n, elem); vector& operator=(const vector &vec); swap(vec);
|
大小操作
1 2 3 4 5 6
| size(); empty(); resize(int num); resize(int num, elem); capacity(); reserve(int len);
|
数据存取
1 2 3 4
| at(int idx); operator[]; front(); back();
|
插入删除
1 2 3 4 5 6
| insert(const_iterator pos, int count,ele); push_back(ele); pop_back(); erase(const_iterator start, const_iterator end); erase(const_iterator pos); clear();
|
deque容器
构造函数
1 2 3 4
| deque<T> deqT; deque(beg, end); deque(n, elem); deque(const deque &deq);
|
赋值操作
1 2 3 4
| assign(beg, end); assign(n, elem); deque& operator=(const deque &deq); swap(deq);
|
大小操作
1 2 3 4
| deque.size(); deque.empty(); deque.resize(num); deque.resize(num, elem);
|
插入删除
1 2 3 4 5 6
| insert(pos,elem); insert(pos,n,elem); insert(pos,beg,end); clear(); erase(beg,end); erase(pos);
|
数据存取
1 2 3 4
| at(idx); operator[]; front(); back();
|
双端插入删除
1 2 3 4
| push_back(elem); push_front(elem); pop_back(); pop_front();
|
stack容器
构造函数
1 2
| stack<T> stkT; stack(const stack &stk);
|
赋值操作
1
| stack& operator=(const stack &stk);
|
大小操作
1 2 3
| push(elem); pop(); top();
|
数据存取
1 2 3
| push(elem); pop(); top();
|
queue容器
构造函数
1 2
| queue<T> queT; queue(const queue &que);
|
赋值操作
1
| queue& operator=(const queue &que);
|
大小操作
1
| queue& operator=(const queue &que);
|
存取插入删除
1 2 3 4
| push(elem); pop(); back(); front();
|
list容器
构造函数
1 2 3 4
| list<T> lstT; list(beg,end); list(n,elem); list(const list &lst);
|
赋值操作
1 2 3 4
| assign(beg, end); assign(n, elem); list& operator=(const list &lst); swap(lst);
|
大小操作
1 2 3 4 5 6 7 8
| size(); empty(); resize(num); 若容器变长,则以默认值填充新位置。 如果容器变短,则末尾超出容器长度的元素被删除。 resize(num, elem); 若容器变长,则以elem值填充新位置。 如果容器变短,则末尾超出容器长度的元素被删除。
|
插入删除
1 2 3 4 5 6 7 8 9 10 11
| push_back(elem); pop_back(); push_front(elem); pop_front(); insert(pos,elem); insert(pos,n,elem); insert(pos,beg,end); clear(); erase(beg,end); erase(pos); remove(elem);
|
数据存取
反转排序
set/multiset容器
构造函数
1 2 3
| set<T> st; mulitset<T> mst; set(const set &st);
|
赋值操作
1 2
| set& operator=(const set &st); swap(st);
|
大小操作
1 2
| set& operator=(const set &st); swap(st);
|
插入删除
1 2 3 4 5
| insert(elem); clear(); erase(pos); erase(beg, end); erase(elem);
|
查找操作
1 2 3 4 5
| find(key); count(key); lower_bound(keyElem); upper_bound(keyElem); equal_range(keyElem);
|
对组(pair)
1 2 3 4 5 6 7 8 9 10 11 12
| pair<string, int> pair1(string("name"), 20); cout << pair1.first << endl; cout << pair1.second << endl;
pair<string, int> pair2 = make_pair("name", 30); cout << pair2.first << endl; cout << pair2.second << endl;
pair<string, int> pair3 = pair2; cout << pair3.first << endl; cout << pair3.second << endl;
|
map/multimap
构造函数
1 2
| map<T1, T2> mapTT; map(const map &mp);
|
赋值操作
1 2
| map& operator=(const map &mp); swap(mp);
|
大小操作
插入删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| map.insert(...); map<int, string> mapStu;
mapStu.insert(pair<int, string>(3, "小张"));
mapStu.inset(make_pair(-1, "校长"));
mapStu.insert(map<int, string>::value_type(1, "小李"));
mapStu[3] = "小刘"; mapStu[5] = "小王"; clear(); erase(pos); erase(beg,end); erase(keyElem);
|
查找操作
1 2 3 4 5
| find(key); count(keyElem); lower_bound(keyElem); upper_bound(keyElem); equal_range(keyElem);
|