c++学习--第七STL-基础

第七部分 STL-基础

1、STL初识

1.1 STL的诞生

image-20220902221048514

1.2 STL基本概念

image-20220902221134122

1.3 STL六大组件

image-20220902221329109

1.4 STL中容器、算法、迭代器

image-20220902221547479

image-20220902221901324

1.5 容器算法迭代器初识

image-20220903220433828

1.5.1 vector存放内置数据类型

image-20220903220513415

#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基本概念

image-20221030213937104

2.1.2string构造函数

image-20221030214802502

#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赋值操作

image-20221030220632017

#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字符串拼接

image-20221101210127451

image-20221101210837929

2.1.5string查找和替换

image-20221101211923701

image-20221101213228039

2.1.6string字符串比较

image-20221104204746500

image-20221104204834563

2.1.7string字符存取

image-20221104205000666

image-20221104205113005

修改可以使用同样的方式

image-20221104205207629

2.1.8string插入和删除*

image-20221104205254720

image-20221104205759889

str的插入和删除下标都是从0开始

2.1.9string子串

image-20221104210132284

2.2vector容器

2.2.1vector基本概念

image-20221104210346981

image-20221104210407911

2.2.2vector构造函数

image-20221104210506203

image-20221104211517965

image-20221104211527943

image-20221104211546778

image-20221104211603959

vector的多种构造方式没有可比性,灵活使用即可

2.2.3vector复制操作

image-20221104211717163

image-20221104212047501

image-20221104212125789

2.2.4 vetcor容量和大小

image-20221104212157953

image-20221104212527120

2.2.5 vetcor插入和删除

image-20221104212610398

image-20221104212856333

image-20221104212834864

2.2.6 vector数据存取

image-20221104212931236

2.2.7 vector互换容器

image-20221104213053625

image-20221104213216717

实际使用 收缩内存

image-20221104213347070

2.2.8 vector 预留空间

image-20221104213506008

image-20221104213718955

2.3 deque容器

2.3.1 deque容器基本概念

image-20221106205645632

image-20221106210409189

2.3.2 deque构造函数

image-20221106210523672

image-20221106210741035

image-20221106210836929

2.3.3 deque赋值操作

image-20221106211006939

image-20221106211106396

2.3.4 deque大小操作

image-20221106211345760

image-20221106211524943

2.3.5 deque插入和删除

image-20221106211722764

2.3.6 deque数据存取

image-20221106212035235

2.3.7 deque排序

image-20221106212122015

image-20221106212305152

总结:sort算法非常实用,使用时包含头文件algorithm即可

2.4 stack容器

2.4.1 stack基本概念

image-20221106212437349

2.4.2 stack常用接口

image-20221106212604992

image-20221106212844832

2.5 queue容器

2.5.1 queue基本概念

image-20221106212956194

2.5.2 queue常用接口

image-20221106213057102

image-20221106222655752

2.6 list容器

2.6.1 list基本概念

image-20221106223152675

image-20221106223410850

image-20221106223446991

image-20221106223132829

2.6.2 list构造函数

image-20221107083140522

image-20221107083335179

2.6.3 list赋值和交换

image-20221107083410447

2.6.4 list大小操作

image-20221107083637275

2.6.5 list插入和删除

image-20221107083829031

image-20221107084200916

2.6.6 list数据存取

image-20221107084406386

image-20221107084827538

2.7.7 list反转和排序

image-20221107084906925

image-20221107085207888

2.7.8 排序案例

image-20221107085233382

image-20221107085620044

image-20221107085700245

image-20221107085713865

2.7 set容器

2.7.1set基本概念

image-20221107151922463

2.7.2 set容器构造

image-20221107152206020

image-20221107152243596

2.7.3 set大小和交换

image-20221107152311173

2.7.4 set插入和删除

image-20221107154224695

image-20221107154637982

2.7.5 set查找和统计

image-20221107154719862

2.7.6 set和multiset区别

image-20221107154904957

image-20221107155707207

image-20221107155734221

总结:

如果不允许插入重复数据可以利用set

如果需要插入重复数据利用multiset

2.7.7 pair对组创建

image-20221107160316031

image-20221107160650956

2.7.8 set容器排序

image-20221107160742746

存放内置函数

image-20221107160947406

image-20221107161046043

存放自定义函数

定义类型

image-20221107161506591

修改内置函数

image-20221107161542236

实例化时使用

image-20221107161616066

总结:

对于自定义数据类型,set必须指定排序规则才可以插入数据

2.8 map容器

2.8.1 map基本概念

image-20221107161719294

2.8.2 map 构造和赋值

image-20221107162931163

image-20221107163606825

image-20221107163547290

总结:map中所有元素都是成对出现,插入数据时候使用对组

2.8.3 map大小和交换

image-20221107164219864

2.8.4 map插入和删除

image-20221107164314292

image-20221107164448614

不建议使用第四种,可以利用key访问value

image-20221107164608412

2.8.5 map统计和查找

image-20221107164657026

2.8.6 map容器排序

image-20221107164743306

image-20221107164833611

仿函数

image-20221107164900903

image-20221107164921988

2.9 案例-员工分组

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4JdS95jz-1667824623562)(C:/Users/62476/AppData/Roaming/Typora/typora-user-images/image-20221107165840580.png)]

image-20221107165906843

3 stl-函数对象

3.1 函数队形

3.1.1函数对象概念

image-20221107195622425

3.1.2 函数对象使用

image-20221107195925759

image-20221107200849040

image-20221107201033049

image-20221107201058846

3.2 谓词

3.2.1 谓词概念

image-20221107201246090

一元谓词

image-20221107201622400

image-20221107201641190

二元谓词

image-20221107201816233

image-20221107201958789

3.3 内建函数对象

3.3.1 内奸函数对象意义

image-20221107202820491

3.3.2 算数仿函数

image-20221107202913251

image-20221107203005403

3.3.3 关系仿函数

image-20221107203053039

image-20221107203203573

3.3.4 逻辑仿函数

image-20221107203342177

image-20221107203510846

猜你喜欢

转载自blog.csdn.net/charles_zhang_/article/details/127738791