开始
STL 的代码从广义上讲分为三类:algorithm(算法)、container(容器)和 iterator(迭代器),几乎所有的代码都采用了模板类和模板函数的方式,这相比于传统的由函数和类组成的库来说。
用 STL 我们就不用反复实现一些代码,提高开发效率。当然这个需要数据结构基础
vector 动态数组
头文件
初始化
1 2 3 4 5 6 7 8 9 10 11 12
| vector<int> ans; vector<structure> ans;
vector<short> ans(n); vector<double> ans(n,2); vector<double> shaow(ans); vector<int> a{1, 2, 3, 4, 5};
vector<int>ans[5]; vector<vectot<int> >num; vector<vector<int> > a(n + 1, vector<int>(m + 1, 0));
|
访问遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> #include "vector" using namespace std; int main() { int n = 5; vector<int> num(5, 1); cout<<num[2];
for (auto i: num) cout << i << " ";
for(int i=0;i<num.size();i++) cout<<num[i]<<' '; return 0; }
|
方法函数
| 函数 |
含义 |
| a.empty() |
判断是否为空,空为真 |
| a.begin() |
返回首元素的迭代器 |
| a.end() |
返回末位元素的迭代器 |
| a.size() |
返回数据个数 |
| a.insert(it,x) |
先任意迭代器插入一个元素 |
| a.push_back() |
尾部添加一个 |
| a.pop_back() |
删除最后一个元素 |
| a.front() |
返回第一个数据 |
| a.clear() |
清除元素 |
| a.resize(n,v) |
改变数组大小为 n, 赋值为 v |
| a.erase(f,l) |
删除 [f,i) 的所有元素 |
| sort(a.begin(),a.end()) |
排序 |
代码
1 2 3 4 5 6 7 8 9 10
| #include <iostream> #include "vector" using namespace std; int main() { int n = 5; vector<int> num(5, 1); for(vector<int>::iterator it=num.begin();it!=num.end();it++) cout<<*it<<' '; return 0; }
|
stack 栈
头文件
初始化
方法函数
| 函数 |
含义 |
| push() |
压栈 |
| pop() |
出栈 |
| empty() |
空为真 |
| top() |
取得栈顶元素 |
| size() |
元素个数 |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream> #include <stack> using namespace std; int main() { stack<int>a; int i; while(!a.empty()) { i=a.top(); cout<<i<<" "; a.pop(); } return 0; }
|
queue 队列
头文件
初始化
方法函数
| 函数 |
含义 |
| front() |
返回队首 |
| back() |
返回队尾元素 |
| pop() |
出队 |
| push() |
进队 |
| size() |
元素个数 |
| empty() |
空队为空 |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <iostream> #include <queue> using namespace std; int main() { queue<int>a; for(int i=0;i<10;i++) a.push(i); int i; while(!a.empty()) { i=a.front(); cout<<i<<' '; a.pop(); } return 0; }
|
deque 双队列
头文件
初始化
方法函数
| 函数 |
含义 |
push_back() push_front() |
入队 |
back() front() |
访问 |
pop_back() pop_back() |
出队 |
| erase(iterator f,iterator l) |
删除 |
| empty() |
空为真 |
| size() |
元素数量 |
| clear() |
清空 |
| sort(iterator f,iterator l) |
排序 |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> #include <deque> #include "algorithm" using namespace std; int main() { deque<int>a; int b[5]={3,8,9,6,2}; for(int i=0;i<5;i++) a.push_front(b[i]); sort(a.begin(),a.end()); int i; while(!a.empty()) { i=a.back(); cout<<i<<' '; a.pop_back(); } return 0; }
|
priority_queue 优先队列
头文件
头文件
1 2
| #include <map> priority_queue<Type, Container, Functional>
|
方法函数
| 函数 |
含义 |
| top() |
队首元素 |
| push() |
入队 |
| pop() |
出队 |
| empty() |
空为真 |
| size() |
元素个数 |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include<iostream> #include <queue> using namespace std; int main() { priority_queue<int> a; priority_queue<int, vector<int>, greater<int> > c; for (int i = 0; i < 5; i++) { a.push(i); c.push(i); } while (!a.empty()) { cout << a.top() << ' '; a.pop(); } cout << endl; while (!c.empty()) { cout << c.top() << ' '; c.pop(); } cout << endl; return 0; }
|
map 映射
头文件
初始化
map<string,int>a;
函数方法
| 函数 |
含义 |
| find(key) |
返回键为 key 的映射的迭代器 当数据存在时,返回数据所在位置的迭代器,数据不存在时,返回 mp.end() |
| erase(it) |
删除迭代器对应的键和值 |
| erase(key) |
根据映射的键删除键和值 |
| erase(first,last) |
删除左闭右开区间迭代器对应的键和值 |
| size() |
返回对数 |
| clear() |
清空所有元素 |
| insert() |
插入元素 |
| empty() |
是否为空 |
| begin() |
第一个元素的迭代器 |
| end() |
返回尾部的迭代器(最后一个元素的下一个地址) |
| rbegin() |
返回最后一个元素的迭代器(地址) |
| rend() |
返回第一个元素的迭代器 |
| rend() |
返回第一个元素的迭代器 |
| count(key) |
查看元素是否存在 |
| lower_bound() |
返回指向键值 >= key 的第一个元素 |
| upper_bound() |
返回指向键值 > key 的第一个元素的迭代器, |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include<iostream> #include <map> using namespace std; int main() { map<int,string>a; a[1]="hello"; a[2]="hi"; cout<<a[1]; cout<<endl; map<int,string>::iterator it; for(it=a.begin();it!=a.end();it++) cout<<it->first<<':'<<it->second<<endl; for(auto i:a) cout<<i.first<<":"<<i.second<<endl; return 0; }
|
set 集合
集合不重合且有序
头文件
方法函数
| 函数 |
含义 |
| begin() |
返回第一个元素的迭代器 |
| end() |
返回最后一个元素的迭代器 |
| rbegin() |
返回逆序迭代器 |
| rend() |
返回逆序迭代器 |
| clear() |
删除 set 容器中的所有的元素 |
| empty() |
判断是否为空 |
| insert() |
插入一个元素 |
| size() |
容器中的元素个数 |
erase(iterator) erase(first,second) |
删除指向的值 |
| erase(key_value) |
删除键值 key_value 的值 |
| find (元素) |
查找 set 中的某一元素,返回迭代器 |
| lower_bound(k) |
返回大于等于 k 的第一个元素的迭代器 |
| upper_bound(k) |
返回大于 k 的第一个元素的迭代器 |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include<iostream> #include <set> using namespace std; int main() { set<int>a; a.insert(8); a.insert(3); a.insert(9); a.insert(7); a.insert(6); a.insert(5); for(set<int>::iterator it=a.begin();it!=a.end();it++) cout<<*it<<" ";
return 0; }
|
pair 对组
pair 只含有两个元素,可以看作是只有两个元素的结构体.
头文件
字符串
头文件
初始化
1 2 3 4 5 6 7 8
| string str1; string str2("123456789"); string str3("12345", 0, 3); string str4("123456", 5); string str5(5, '2'); string str6(str2, 2);
|
函数方法
| 函数 |
含义 |
| sort(s.begin(),s.end()); |
排序 |
size() length() |
长度 |
| max_size() |
返回 string 对象最多包含的字符数 |
| capacity() |
重新分配内存之前,string 对象能包含的最大字符数 |
| push_back() |
末尾插入 |
| insert(pos,element) |
插入 |
| append(str) |
末尾添加字符串 |
| erase(it)erase(iterator p) |
删除指定字符 |
| erase(iterator first, iterator last) |
删除字符串中迭代器区间所有字符 |
| clear() |
清空 |
| replace(pos,n,str) |
把当前字符串从索引 pos 开始的 n 个字符替换为 str |
| replace(pos,n,x,c) |
把当前字符串从索引 pos 开始的 n 个字符替换为 x 个字符 c |
| replace(it1,it2,str) |
把当前字符串 [it1,it2) 区间替换为 str |
| tolower() |
转换为小写 |
| toupper() |
转换为大写 |
| substr(pos,n) |
截取从 pos 索引开始的 n 个字符 |
find (str, pos)
find (c, pos) |
在当前字符串的 pos 索引位置 (默认为 0) 开始,查找子串 str,返回找到的位置索引,-1 表示查找不到子串 |
| rfind (str, pos) |
在当前字符串的 pos 索引位置开始,反向查找子串 s,返回找到的位置索引 |
| find_first_of (str, pos) |
在当前字符串的 pos 索引位置 (默认为 0) 开始,查找子串 s 的字符 |
| .find_first_not_of (str,pos) |
在当前字符串的 pos 索引位置 (默认为 0) 开始,查找第一个不位于子串 s 的字符 |
| find_last_of(str, pos) |
当前字符串的 pos 索引位置开始,查找最后一个位于子串 s 的字符 |
| find_last_not_of ( str, pos) |
在当前字符串的 pos 索引位置开始,查找最后一个不位于子串 s 的字符 |
bitsei 位组
头文件
初始化
1 2 3 4
| bitset < n >a; bitset < n >a(b); bitset < n >a(s,pos,n); bitset < n >a(s);
|
函数方法
| 函数 |
含义 |
| any() |
是否存在置为 1 的二进制位,有返回 true |
| none() |
b 中是否没有 1 |
| count() |
1 的个数 |
| size() |
二进制位的个数 |
| test(pos) |
在 pos 位置是否为 1,是返回 true |
| b.set() |
所有位都置为 1 |
| reset() |
所有位都置为 0 |
| reset(pos) |
pos 位置置为 0 |
| flip() |
所有二进制位取反 |
| flip(pos) |
pos 位置取反 |
| to_ulong() |
同样的二进制位返回一个 unsigned long 值 |
bitset 也支持位运算
array 数组
头文件
初始化
1 2 3 4
| array<int, 100> a; array<int, 100> a{}; array<int, 100> a{1, 2, 3}; array<int, 100> a = {1, 2, 3};
|
访问遍历
1 2 3 4 5 6 7 8 9 10 11 12 13
| array<int, 4> a = {1, 2, 3, 4}; for(int i = 0; i < 4; i++) cout << a[i] << " \n"[i == 3];
for(auto i : a) cout << i << " ";
array<int, 4> a = {1, 2, 3, 4}; int res = a.at(1) + a.at(2); cout << res << "\n";
get<1>(a) = x;
|
方法函数
| 函数 |
含义 |
| begin() |
第一个元素的访问迭代器 |
| end() |
返回容器最后一个元素之后一个位置的访问迭代器 |
| rbegin() |
返回最后一个元素的访问迭代器 |
| rend() |
返回第一个元素之前一个位置的访问迭代器 |
| size() |
返回容器中元素的数量 |
| max_size() |
返回容器可容纳元素的最大数量 |
| empty() |
判断容器是否为空 |
| at(n) |
返回容器中 n 位置处元素的引用 |
| front() |
返回容器中第一个元素的直接引用 |
| back() |
返回容器中最后一个元素的直接引用 |
| data() |
返回一个指向容器首个元素的指针 |
| fill(x) |
将 x 这个值赋值给容器中的每个元素, 相当于初始化 |
| array1.swap(array2) |
交换 array1 和 array2 容器中的所有元素,但前提是它们具有相同的长度和类型 |
| sort() |
排序 |
tuple 元组
头文件
初始化
1 2 3 4 5 6 7
| tuple<int, int, string> t1; t1 = make_tuple(1, 1, "hahaha");
tuple<int, int, int, int> t2(1, 2, 3, 4);
auto p = make_pair("wang", 1); tuple<string, int> t3 {p};
|
操作
读取修改
1 2
| int first = get<0>(t); get<0>(t) = 1;
|
获取元素个数
1 2
| tuple<int, int, int> t(1, 2, 3); cout << tuple_size<decltype(t)>::value << "\n";
|
解包
1 2 3 4 5
| int one, three; string two; tuple<int, string, int> t(1, "hahaha", 3); tie(one, two, three) = t; cout << one << two << three << "\n";
|