#STL概述
STL诞生
长久以来,软件界一直希望建立一种可重复利用的东西。
C++的面向对象和泛型编程思想,目的就是复用性的提升。
大多情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作。
为了建立数据结构和算法的一套标准,诞生了STL。
STL的基本概念
STL(Standard Template Library, 标准模板库)
STL从广义上分为:容器(container)
算法(algorithm)
迭代器(iterator)
容器和算法之间通过迭代器进行无缝连接
STL几乎所有的代码都采用了模板类或者模板函数
STL六大组件
STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器
- 容器:各种数据结构,如
vector、list、deque、set、map
等,用来存放数据 - 算法:各种常用算法,如
sort、find、copy、for_each
等 - 迭代器:扮演了容器与算法之间的胶合剂
- 仿函数:行为类似函数,可作为算法的某种策略
- 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西
- 空间适配器:负责空间的配置与管理
#STL-容器
STL-容器
容器:数据的存储
STL容器就是将运用最广泛的一些数据结构实现出来。
常用的数据结构:数组
、链表
、树
、栈
、队列
、集合
、映射表
等,这些容器在STL中分为序列容器、关联容器和哈希容器三种,我们之前学过的string就是STL容器的一种。
vector向量
vector容器是STL中最常用的容器之一,可以看做是对C++中普通数组的"升级版"。而和普通数组的不同之处在于,普通数组是静态数组(容量固定的数组),而vector实现的是一个动态数组,即可以进行元素的插入和删除,在此过程中,vector会动态调整所占用的内存空间,整个过程无需人工干预。
vector常被称为向量容器,因为该容器擅长在尾部插入或删除元素,在常量时间内就可以完成,时间复杂度为O(1),而对于在容器头部或者中部插入或删除元素,则花费时间要长一些(移动元素需要耗费时间),时间复杂度为线性阶O(n)。
vector的创建和使用
vector容器以类模板 vector<T>
(T表示存储元素的类型)的形式定义在头文件中,并位于std命名空间中。因此,在创建该容器之前,代码中需包含如下内容:
#include <vector>
using namespace std;
接下来开始vector的创建和使用。
#include <iostream>
#include <vector>
using namespace std;
// vector的初始化操作
void test01(){
// 默认构造函数 第一种方式
//vector<int> v;
// 第二种方式 通过初始化列表构造 不常用
//vector<int> v = {1, 2, 3, 4, 5, 6};
// 第三种方式 初始化元素个数为10个,每个元素自动初始化为0
//vector<int> v(10);
vector<int> v(10, 1);
cout << "v数组中元素的个数:" << v.size() << endl;
//向数组尾部插入元素
v.push_back(10);
v.push_back(20);
v.push_back(30);
//从尾部删除元素
v.pop_back();
cout << "v数组中元素的个数:" << v.size() << endl;
for(int i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
}
int main() {
// 静态数组
//int a[110];
// vector动态数组
//vector<int> v;
test01();
return 0;
}
运行结果:
v数组中元素的个数:10
v数组中元素的个数:12
1 1 1 1 1 1 1 1 1 1 10 20
#STL-迭代器
迭代器
迭代器:容器和算法的粘合剂
迭代器提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴漏该容器的内部表示方式。每个容器都有自己专属的迭代器。
迭代器使用非常类似于指针,初学阶段我们可以先理解迭代器为指针
通过迭代器访问vector
- 通过迭代器直接访问vector
- 调用for_each()算法,传入迭代器
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int v){
cout << v << " ";
}
// vector的初始化操作
void test01(){
// *it
vector<int> v{1, 2, 3, 4, 5 };
// vector int 作用域下的迭代器
// v.begin()返回v数组中首元素的位置
// v.end()返回v数组中尾元素的下一位的位置
vector<int>::iterator it = v.begin();
//cout << *it << endl;
// it != v.end()走到尾元素下一位置,跳出循环
/*for(; it != v.end(); it++){
cout << *it << " ";
}*/
// 凡是传迭代器区间作为参数的,
// 皆要遵循前闭后开原则
for_each(v.begin(), v.end(), print);
}
int main() {
test01();
return 0;
}
运行结果:
1 2 3 4 5
#STL-算法
算法
算法:解决问题的方法
在有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法(Algorithms)算法分为:质变算法和非质变算法。
质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等。
非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等。
STL-算法
STL对一些常用的经典算法进行了封装,使用时直接调用即可。
使用前需要导入头文件
#include <algorithm>
min()、max()、swap()
min()
获取两个变量中的最小值
max()
获取两个变量中的最大值
swap()
交换两个变量的值
最大最小值
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test01(){
int x;
int maxv = 0, minv = 0x3f3f3f3f;
for(int i = 0; i < 8; i++){
cin >> x;
maxv = max(maxv, x);
minv = min(minv, x);
}
cout << maxv << " " << minv << endl;
}
int main() {
test01();
return 0;
}
运行结果:
1 2 3 4 5 6 7 8
8 1
min_element()、max_element()
min_element()获取序列中的最小值
max_element()获取序列中的最大值
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test01(){
vector<int> v{1,2,3,4,5};
// v.begin(), v.end()开始,结束位置,返回迭代器,需要*解引用
int maxv = *max_element(v.begin(), v.end());
int minv = *min_element(v.begin(), v.end());
cout << maxv << " " << minv << endl;
}
int main() {
test01();
return 0;
}
运行结果:
5 1
排序算法sort()
之前学过了冒泡排序算法,若每次做基础的操作时都要手写排序算法,编码效率会比较低,直接基于快速排序封装的sort()算法,可以提高编码效率。
接下来使用sort()算法分别对静态数组和动态数组进行排序。
静态数组排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test01(){
int a[] = {5,4,3,1,2};
sort(a, a + 5);
for(int i = 0; i < 5; i++) cout << a[i] << " ";
}
int main() {
test01();
return 0;
}
运行结果:
1 2 3 4 5
vector动态数组排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test01(){
vector<int> v = {5,4,3,1,2};
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
}
int main() {
test01();
return 0;
}
运行结果:
1 2 3 4 5
sort()和自定义类型
若是用sort()对自定义类型-结构体元素进行排序,必须完成具体的比较规则。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct stu{
int id;
string name;
};
bool cmp(stu A, stu B){
return A.id < B.id;
}
void test01(){
vector<stu> v;
int n; cin >> n;
for(int i = 0; i < n; i++){
int id; string name;
cin >> id >> name;
v.push_back({id, name});
}
cout << "------------------" << endl;
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < v.size(); i++){
cout << v[i].id << " " << v[i].name << endl;
}
}
int main() {
test01();
return 0;
}
运行结果:
3
3 laowang
2 dawang
1 xiaowang
------------------
1 xiaowang
2 dawang
3 laowang