C++经典问题_17 STL(一)

一. STL概述

① STL的诞生
  1. 长久以来,软件开发都希望可以创建一种可以重复利用的东西
  2. C++的面向对象和泛型编程思想,目的就是复用性的提升
  3. 大多数情况下,数据结构和算法都未能有一套标准,导致被迫从事大量的重复工作
  4. 为了建立数据结构和算法的一套标准,诞生了STL
② STL的基本概念
  1. STL(Standard Template Library): 标准模板库
  2. STL从广义上分为: 容器(container), 算法(algorithm) 迭代器(iterator)
  3. 容器和算法之间通过迭代器进行无缝连接
  4. STL几乎所有的代码都采用了模板类或者模板函数
③ STL六大组件
  1. 容器: 各种数据结构,如vector, list, deque,set,map
  2. 算法: 各种常用的算法 sort,find,copy,for_each
  3. 迭代器: 扮演了容器和算法之间的胶合剂
  4. 仿函数: 行为类似函数,可以协助算法的某种策略
  5. 适配器: 一种用来修饰容器或者仿函数或者迭代器接口的东西
  6. 空间配置器: 负责空间的配置和管理

二. 容器算法迭代器

① 容器概述
  1. STL容器就是将运用最广泛的一些数据结构实现出来
  2. 常用数据结构: 数组,链表,树,栈,集合,映射表等
  3. 分类: 序列式容器和关联式容器
  4. 序列式容器: 强调值的排序,序列式容器中的每个元素均有固定的位置
  5. 关联式容器: 二叉树结构,各元素之间没有严格物理上的顺序关系
② 算法
  1. 问题的解决办法
  2. 通过有限的步骤,解决逻辑或数学上的问题.
  3. 分类: 质变算法和非质变算法
  4. 质变算法: 是指运算过程中会更改区间内元素的内容.如: 拷贝,替换,删除等等.
  5. 非质变算法: 是指运算过程中不会改变区间内元素的内容,如:查找,计数,遍历,寻找极值等.
③ 迭代器
  1. 容器和算法之间的粘合剂
  2. 算法通过迭代器才能访问容器中的元素
  3. 每个容器都有自己专属的迭代器
  4. 分类:输入迭代器 输出迭代器,前向迭代器,双向迭代器,随机访问迭代器

四. Vector容器

① 概述
  1. 可以理解为数组,可以插入数据,遍历数据
  2. 容器: vector
  3. 算法: for_each: 需要包含算法头文件
  4. 迭代器: 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容器嵌套容器
  1. 类似二维数组,容器里面存放的数据类型依旧还是容器
  2. 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值