文章目录
一. 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
容器就是将运用最广泛的一些数据结构实现出来常用数据结构:
数组,链表,树,栈,集合,映射表等分类:
序列式容器和关联式容器序列式容器:
强调值的排序,序列式容器中的每个元素均有固定的位置关联式容器:
二叉树结构,各元素之间没有严格物理上的顺序关系
② 算法
- 问题的解决办法
- 通过有限的步骤,解决逻辑或数学上的问题.
分类:
质变算法和非质变算法质变算法:
是指运算过程中会更改区间内元素的内容.如: 拷贝,替换,删除等等.非质变算法:
是指运算过程中不会改变区间内元素的内容,如:查找,计数,遍历,寻找极值等.
③ 迭代器
- 容器和算法之间的粘合剂
- 算法通过迭代器才能访问容器中的元素
- 每个容器都有自己专属的迭代器
分类:
输入迭代器 输出迭代器,前向迭代器,双向迭代器,随机访问迭代器
四. Vector容器
① 概述
- 可以理解为数组,可以插入数据,遍历数据
容器:
vector
算法:
for_each:
需要包含算法头文件迭代器:
vector<int>::iterator
可以理解为指针
② 案例
for_each语法
for_each(v.begin(),v.end(),somefunc); // 其中somefunc是一个回调函数,在遍历的时候会调用这个函数
vector<>::iterator:
是一个迭代器,可以理解为是一个指针
v.begin():
容器的起始位置
v.end():
容器的结束位置的下一个位置
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> create_vector(void)
{
// 创建int类型的容器类
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
return v; // 返回这个迭代器
}
// 使用while循环来遍历容器,很繁琐,不建议使用,但是可以使用
void iterator_vector_01(vector<int> v)
{
// 迭代容器,第一种采用循环的方式
vector<int>::iterator beginPtr = v.begin();
vector<int>::iterator endPtr = v.end();
while (beginPtr != endPtr)
{
cout << *beginPtr << " ";
beginPtr++;
}
cout << endl;
}
// 使用for循环
void iterator_vector_02(vector<int> v)
{
// 使用for循环的方式
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void print_info(int val)
{
cout << val << " ";
}
// 使用迭代器算法 for_each
void iterator_vector_03(vector<int> v)
{
for_each(v.begin(), v.end(),print_info);
cout << endl;
}
void test_iterator(void)
{
// 测试迭代器
vector<int> v;
v = create_vector();
iterator_vector_01(v);
iterator_vector_02(v);
iterator_vector_03(v);
}
int main()
{
test_iterator();
system("pause");
return 0;
}
③ vector中存放自定义的数据类型
1)遍历的时候迭代器是什么类型由创建的模板的类型决定
2)可以使用取*
号方式访问,也可以通过指针的方式->
访问
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
};
void test_01(void)
{
Person p1("AAA", 10);
Person p2("BBB", 20);
Person p3("CCC", 30);
Person p4("DDD", 40);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//for循环访问
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
// 通过*it来访问, *it的类型就是vector里面存放的数据的类型
cout << "姓名: " << (*it).mName << " 年龄: " << (*it).mAge << endl;
// 直接通过it指针来访问,it指向的类型是<Person>类型
cout << "姓名: " << it->mName << " 年龄: " << it->mAge << endl;
}
cout << "*************************************" << endl;
}
void test_02(void)
{
Person p1("AAA", 10);
Person p2("BBB", 20);
Person p3("CCC", 30);
Person p4("DDD", 40);
vector<Person*> v; // 存放指向Person的指针类型
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
for (vector<Person *>::iterator it = v.begin(); it != v.end(); it++)
{
// 通过*it来访问
cout << "姓名: " << (*it)->mName << " 年龄: " << (*it)->mAge << endl;
// 通过**it来访问
cout << "姓名: " << (**it).mName << " 年龄: " << (**it).mAge << endl;
}
cout << "*************************************" << endl;
}
int main()
{
test_01();
test_02();
system("pause");
return 0;
}
④ vector容器嵌套容器
- 类似二维数组,容器里面存放的数据类型依旧还是容器
vector<vector<int>> v:
第一个尖括号里面存放的是容器类型,小容器里面存放的是int
类型
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include <vector>
void test_01(void)
{
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 + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
// 将小容器插入到大容器中
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 << "\n";
}
}
int main()
{
test_01();
system("pause");
return 0;
}