第七部分 STL-基础
1、STL初识
1.1 STL的诞生
1.2 STL基本概念
1.3 STL六大组件
1.4 STL中容器、算法、迭代器
1.5 容器算法迭代器初识
1.5.1 vector存放内置数据类型
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法的头文件
//vector容器存放内置数据类型
void myPrint(int val) {
cout << val << endl;
}
void test01() {
//创建了一个vector容器,数组
vector<int> v;
//想容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//通过迭代器访问容器中的数据‘
//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;
}*/
//第三种遍历方式 利用stl提供遍历算法
for_each(v.begin(), v.end(), myPrint);//回调函数
}
int main() {
test01();
}
1.5.2 vector存放自定义数据类型
学习目标:vector中存放自定义数据类型,并打印输出
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>//标准算法的头文件
#include <string>
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
vector<Person>v;
Person p1("aaa", 11);
Person p2("bbb", 22);
Person p3("ccc", 33);
Person p4("ddd", 44);
Person p5("eee", 55);
//添加数据
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 << "姓名: " << (*it).m_Name << "年龄: " << (*it).m_Age << endl;
}
}
void test02() {
vector<Person*>v;
Person p1("aaa", 11);
Person p2("bbb", 22);
Person p3("ccc", 33);
Person p4("ddd", 44);
Person p5("eee", 55);
//添加数据
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++) {
Person * p = (*it)
cout << "姓名: " << p->m_Name << "年龄: " << p->m_Age << endl;
}
}
int main() {
//test01();
test02();
}
1.5.3 Vector容器嵌套容器
学习目标:容器中嵌套容器,我们将所有数据进行遍历输出
#include<iostream>
using namespace std;
#include <vector>
void test01() {
vector <vector<int>>v;
//创建小容器
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
vector<int>v5;
//向小容器添加数据
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++) {
// (*it)------容器 vector<int>
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main() {
test01();
}
2、STL-常用容器
2.1 string容器
2.1.1string基本概念
2.1.2string构造函数
#include<iostream>
using namespace std;
#include <string>
//string 的构造
void test01() {
string s1;//默认构造
const char * str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "S3 = " << s3 << endl;
string s4(10, s3);
cout << "s4 = " << s4 << endl;
}
int main() {
test01();
}
总结:string的多种构造方式没有可比性,怎么好用怎么来
2.1.3string赋值操作
#include<iostream>
using namespace std;
//string赋值
void test01() {
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
string str3;
str3 = "A";
string str4;
str4.assign("hell c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++", 5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
string str7;
str7.assign(10,"w");
}
int main() {
test01();
}
总结:string的赋值方式很多,operator=这种方式比较实用的
2.1.4string字符串拼接
2.1.5string查找和替换
2.1.6string字符串比较
2.1.7string字符存取
修改可以使用同样的方式
2.1.8string插入和删除*
str的插入和删除下标都是从0开始
2.1.9string子串
2.2vector容器
2.2.1vector基本概念
2.2.2vector构造函数
vector的多种构造方式没有可比性,灵活使用即可
2.2.3vector复制操作
2.2.4 vetcor容量和大小
2.2.5 vetcor插入和删除
2.2.6 vector数据存取
2.2.7 vector互换容器
实际使用 收缩内存
2.2.8 vector 预留空间
2.3 deque容器
2.3.1 deque容器基本概念
2.3.2 deque构造函数
2.3.3 deque赋值操作
2.3.4 deque大小操作
2.3.5 deque插入和删除
2.3.6 deque数据存取
2.3.7 deque排序
总结:sort算法非常实用,使用时包含头文件algorithm即可
2.4 stack容器
2.4.1 stack基本概念
2.4.2 stack常用接口
2.5 queue容器
2.5.1 queue基本概念
2.5.2 queue常用接口
2.6 list容器
2.6.1 list基本概念
2.6.2 list构造函数
2.6.3 list赋值和交换
2.6.4 list大小操作
2.6.5 list插入和删除
2.6.6 list数据存取
2.7.7 list反转和排序
2.7.8 排序案例
2.7 set容器
2.7.1set基本概念
2.7.2 set容器构造
2.7.3 set大小和交换
2.7.4 set插入和删除
2.7.5 set查找和统计
2.7.6 set和multiset区别
总结:
如果不允许插入重复数据可以利用set
如果需要插入重复数据利用multiset
2.7.7 pair对组创建
2.7.8 set容器排序
存放内置函数
存放自定义函数
定义类型
修改内置函数
实例化时使用
总结:
对于自定义数据类型,set必须指定排序规则才可以插入数据
2.8 map容器
2.8.1 map基本概念
2.8.2 map 构造和赋值
总结:map中所有元素都是成对出现,插入数据时候使用对组
2.8.3 map大小和交换
2.8.4 map插入和删除
不建议使用第四种,可以利用key访问value
2.8.5 map统计和查找
2.8.6 map容器排序
仿函数
2.9 案例-员工分组
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4JdS95jz-1667824623562)(C:/Users/62476/AppData/Roaming/Typora/typora-user-images/image-20221107165840580.png)]
3 stl-函数对象
3.1 函数队形
3.1.1函数对象概念
3.1.2 函数对象使用
3.2 谓词
3.2.1 谓词概念
一元谓词
二元谓词
3.3 内建函数对象
3.3.1 内奸函数对象意义
3.3.2 算数仿函数
3.3.3 关系仿函数
3.3.4 逻辑仿函数