Standard Library 标准库
Standard Template Library 标准模板库
1. 容器 containers
2. 迭代器 iterators
3. 算法 algorithms
4. 函数器 functional
0.1 函数模板、类模板 与 * 乘法运算符 重载
#include
template<typename T>
T square(T x){
return x*x;
}
template<typename T>
class BoVector{
T arry[1000];
int size;
public:
BoVector(): size(0) {};
void push(T x){ arry[size] = x; size++; }
T get(int i) const {return arry[i]; }
int getsize() const {return size;}
void print() const { for(int i=0; i<size; i++){std::cout << arry[i] << std::endl;} }
};
template<typename T>
BoVector<T> operator*(const BoVector<T>& bv1, BoVector<T>& bv2){
BoVector<T> ret;
for(int i=0; i<bv1.getsize(); i++){
ret.push(bv1.get(i) * bv2.get(i));
}
return ret;
}
int main(){
std::cout << square(5) << std::endl;
std::cout << square(5.5) << std::endl;
BoVector<int> bv;
bv.push(1);
bv.push(2);
bv.push(3);
bv.push(4);
bv.print();
bv = square(bv);
bv.print();
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
0.2容器 迭代器 算法 示例
#include
#include
#include
using namespace std;
int main(){
vector<int> vec_i;
vec_i.push_back(5);
vec_i.push_back(2);
vec_i.push_back(0);
vector<int>::iterator iter_beg = vec_i.begin();
vector<int>::iterator iter_end = vec_i.end();
for(vector<int>::iterator it = iter_beg; it!=iter_end; ++it){
cout << *it << endl;
}
sort(iter_beg+1, iter_end);
for(vector<int>::iterator it = iter_beg; it!=iter_end; ++it){
cout << *it << endl;
}
sort(iter_beg, iter_end);
for(vector<int>::iterator it = iter_beg; it!=iter_end; ++it){
cout << *it << endl;
}
return 0;
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
1. 容器
1.1 序列容器
vector向量 deque双向队列 双向链表list 单向链表forward_list array数组
#include
#include
#include
#include
#include
a. vector向量 动态数组
||| ........... ----->
#include
#include
using namespace std;
int main(){
vector<int> vec_i;
vec_i.push_back(5);
vec_i.push_back(2);
vec_i.push_back(0);
cout << vec_i[2] << endl;
cout << vec_i.at(1) << endl;
for(int i = 0; i < vec_i.size(); ++i){
cout << vec_i[i] << " ";
}
cout << endl;
for(vector<int>::iterator iter = vec_i.begin(); iter != vec_i.end(); ++iter){
cout << *iter << " ";
}
cout << endl;
int* p = &vec_i[0];
for(int i = 0; i < vec_i.size(); ++i){
cout << p[i]<< " ";
}
cout << endl;
if(vec_i.empty()) cout << "vec_i has no num" << endl;
cout << vec_i.size() << endl;
vector<int> vec_i2(vec_i);
vec_i.clear();
vec_i2.swap(vec_i);
return 0;
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
b. deque 双向队列
<-----|..................|----->
队首 队尾
#include
using namespace std;
deque<int> deq = {5, 2, 0};
deq.push_back(5);
deq.push_front(0);
cout << deq[1] ;
cout << deq.at(2) ;
deque<int>::iterator iter = deq.begin() + 3;
cout << *iter ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
c.双向链表list
双向链表,包含前后指针,和数据域,数据不连续,浪费访存,多包含两个指针变量,浪费内存
#include
#include
#include
using namespace std;
int main(){
list<int> mylist = {5, 2, 0};
mylist.push_back(1);
mylist.push_back(3);
mylist.push_back(1);
mylist.push_back(4);
mylist.push_front(5);
list<int>::iterator itr = find(mylist.begin(), mylist.end(), 2);
cout << *itr << endl;
mylist.insert(itr,6);
cout << *itr << endl;
itr++;
cout << *itr << endl;
mylist.erase(itr);
cout << *itr << endl;
for(list<int>::iterator itr1 = mylist.begin(); itr1 != mylist.end();++itr1){
cout << *itr1 << endl;
}
return 0;
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
d. 单向链表forward_list
e. array数组
1.2 有序关联容器(红黑二叉树)
set, multiset 单值树集合 multi 关键字可以重复出现
map, multimap 配对双值树集合
#inlcude
#inlcude
#inlcude
1.3 无序关联容器(哈希表)
unordered set/multiset 单值桶集合
unordered map/multimap 配对双值桶集合
#inlcude
#inlcude
#inlcude
#inlcude
// 和另一个库里的类似
hash_map
hash_set
hash_multimap
hash_multiset