[C++提高编程](三):STL初识
目录
STL的诞生
STL基本概念
STL六大组件
STL中的容器、算法、迭代器
容器:置物之所也
算法:问题之解法也
迭代器:容器和算法之间的粘合剂
初识
STL的诞生
- C++的面向对象和泛型编程思想,目的就是提升复用性
- 为了建立数据结构和算法的一套标准,诞生了STL
STL基本概念
- STL(Standard Template Library),标准模板库
- STL从广义上分为:容器(container)、算法(algorithm)、迭代器(iterator)
- 容器和算法之间通过迭代器进行无缝连接
- STL几乎所有的代码都采用了模板类或者模板函数
STL六大组件
容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
- 容器,各种数据结构,如:vector、list、deque、set、map等用来存放数据
- 算法,各种常用算法,如:sort、find、copy、for_each等
- 迭代器,扮演了容器与算法之间的胶合剂
- 仿函数,行为类似函数,可作为算法的某种策略
- 适配器, 一种用来修饰容器或者仿函数或迭代器接口的东西
- 空间配置器,负责空间的配置以及管理
STL中的容器、算法、迭代器
容器:置物之所也
序列式容器:强调值的排序,每个元素均有固定的位置
关联式容器:二叉树结构,各元素直接没有严格的物理上的顺序关系
算法:问题之解法也
有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(algorithm)
质变算法:运算过程中会更改区间内的元素内容,如:拷贝、删除、替换等
非质变算法:运算过程中不会更改区间内的元素内容,如:查找、计数、遍历等
迭代器:容器和算法之间的粘合剂
提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表达方式
每个容器都有自己专属的迭代器
迭代器非常类似于指针,初学阶段可以理解为指针
种类 | 功能 | 支持运算 |
输入迭代器 | 对数据只读访问 | 只读,支持++、==、!= |
输出迭代器 | 对数据只写访问 | 只写,支持++ |
前向迭代器 | 读写操作,并能向前推进迭代器 | 读写,支持++、==、!= |
双向迭代器 | 读写操作,并能向前或向后操作 | 读写,支持++、-- |
随机访问迭代器 | 读写操作,可以跳跃式访问任意数据,功能最强 | 读写,支持++、--、[n]、-n、<、<=、>、>= |
初识
STL最常用的容器是vector,可以理解为数组
vector存放内置数据类型,容器:vector,算法:for_each,迭代器:vector<int>::iterator
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //标准算法头文件
using namespace std;void myPrint(int val)
{cout << val << endl;
}void test(void)
{vector<int> v; //定义一个v容器v.push_back(10);v.push_back(20);v.push_back(30);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;}//第三遍历 利用遍历算法 包含头文件:algorithmfor_each(v.begin(), v.end(), myPrint);
}int main()
{test();return 0;
}
vector存放自定义数据类型,并打印输出
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //标准算法头文件
using namespace std;class Person
{
public:string m_name;int m_age;Person(string name, int age){this->m_name = name;this->m_age = age;}
};void myPrint(Person p)
{cout << "2姓名:" << p.m_name << ",2年龄:" << p.m_age << endl;
}void myPrint02(Person *p)
{cout << "p2姓名:" << p->m_name << ",p2年龄:" << p->m_age << endl;
}void test(void)
{vector<Person> m;Person p1("aaa", 10);Person p2("ccc", 30);Person p3("ddd", 20);Person p4("eee", 60);Person p5("rrr", 70);m.push_back(p1);m.push_back(p2);m.push_back(p3);m.push_back(p4);m.push_back(p5);//遍历1for (vector<Person>::iterator it = m.begin(); it != m.end(); it++){cout << "1姓名:" << (*it).m_name << ",1年龄:" << (*it).m_age << endl;}//遍历2for_each(m.begin(), m.end(), myPrint);
}void test02(void)
{vector<Person*> m;Person p1("aaa", 10);Person p2("ccc", 30);Person p3("ddd", 20);Person p4("eee", 60);Person p5("rrr", 70);m.push_back(&p1);m.push_back(&p2);m.push_back(&p3);m.push_back(&p4);m.push_back(&p5);//遍历1for (vector<Person*>::iterator it = m.begin(); it != m.end(); it++){Person* p = *it;cout << "p1姓名:" << p->m_name << ",p1年龄:" << p->m_age << endl;}//遍历2for_each(m.begin(), m.end(), myPrint02);
}int main()
{test();test02();return 0;
}
推荐文章:[C++提高编程](二):模板--类模板