【C++进阶笔记二】STL标准模板库(Standard Template Library)
一、STL 初识1. STL 六大 组件2. 容器、算法、迭代器3. 容器vector、算法for_each、迭代器 vector\< int>::interator4. Vector 存放自定义数据类型5. Vector 中嵌套 Vector6. string
一、STL 初识
STL 的主要目的是建立数据结构与算法的一套标准,提高代码复用性,减少编程人员的重复工作。 STL 中几乎所有的代码都采用 了模板类或都模板函数。
1. STL 六大 组件
STL 大体分为六大组件,分别为:容器、算法、迭代器、仿函数,适配器(配接器)、空间配置器。
(1)、容器 各种数所结构,如 vector、list、deque、set、map 等,用来存放数据
(2)、算法 各种常用算法,如 sort、find、copy、for_each 等
(3)、迭代器 扮演了容器与算法之间的胶合剂
(4)、仿函数 行为 类似函数,可作为算法的某种策略。
(5)、适配器 一种用来修饰容器或者仿函数或迭代中存在的接口的东西。
(6)、空间适配器 负责空间的配置与管理
2. 容器、算法、迭代器
容器分为 序列式容器 和 关联式容器 两种。
序列式容器: 强调值的排序,每个元素均有固定的位置。关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系
算法分为质变算法 和 非质变算法
质变算法:运算过程中会更改区间内元素的内容,如拷贝、替换、删除等。非质变算法:运算过程中不会更改区间内的元素内容,如查找、计数,遍历等。
每个容器都有专属的迭代器,算法必须通过迭代器 才能访问容器。 常用的容器中的迭代器均为 双向迭代器(能读写且前后操作) 和 随机访问迭代器(能读写且跳跃操作,功能最强)。
3. 容器vector、算法for_each、迭代器 vector< int>::interator
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std
;
void Print(int val
){
cout
<< val
<< endl
;
}
int main()
{
vector
<int> v
;
v
.push_back(10);
v
.push_back(20);
v
.push_back(30);
v
.push_back(40);
vector
<int>::iterator itBegin
= v
.begin();
vector
<int>::iterator itEnd
= v
.end();
while (itBegin
!= itEnd
){
cout
<< *itBegin
<< endl
;
itBegin
++;
}
for(vector
<int>::iterator it
= v
.begin(); it
!= v
.end(); it
++){
cout
<< *it
<< endl
;
}
for_each(v
.begin(), v
.end(), Print
);
system("pause");
return 0;
}
4. Vector 存放自定义数据类型
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std
;
class Person
{
public
:
Person(string name
, int age
) :m_Name(name
), m_Age(age
) { }
string m_Name
;
int m_Age
;
};
void printf_person(Person
& p
){
cout
<< "name=" << p
.m_Name
<< " age=" << p
.m_Age
<< endl
;
}
int main()
{
vector
<Person
> v
;
Person
p1("a", 10);
Person
p2("b", 11);
Person
p3("c", 12);
Person
p4("d", 13);
Person
p5("e", 14);
v
.push_back(p1
);
v
.push_back(p2
);
v
.push_back(p3
);
v
.push_back(p4
);
v
.push_back(p5
);
for (vector
<Person
>::iterator it
= v
.begin(); it
!= v
.end(); it
++)
{
cout
<< "name=" << it
->m_Name
<< " age=" << it
->m_Age
<< endl
;
}
for_each(v
.begin(), v
.end(), printf_person
);
system("pause");
return 0;
}
5. Vector 中嵌套 Vector
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std
;
int main()
{
vector
< vector
<int> > v
;
vector
<int>v1
;
vector
<int>v2
;
vector
<int>v3
;
vector
<int>v4
;
for (int i
= 0; i
< 4; i
++)
{
v1
.push_back(i
+ 1);
v2
.push_back(i
+ 5);
v3
.push_back(i
+ 9);
v4
.push_back(i
+ 13);
}
v
.push_back(v1
);
v
.push_back(v2
);
v
.push_back(v3
);
v
.push_back(v4
);
for (vector
<vector
<int>>::iterator it
= v
.begin(); it
!= v
.end(); it
++)
{
for (vector
<int>::iterator vit
= (*it
).begin(); vit
!= (*it
).end(); vit
++)
{
cout
<< *vit
<< " ";
}
cout
<< endl
;
}
system("pause");
return 0;
}
6. string
string 是一个 类,类内部封装了char *,并管理所分配的内存。
string();
string(const char* s
);
string(const string
&str
);
string(int n
, char c
);
后面的set,pare,map等都比较好理,就不做笔记了,先这样吧。
后面如果碰到不会的,再做笔记记录下。