c++ learning--Seventh STL-Basics

Part 7 STL-Basics

1. First introduction to STL

1.1 The birth of STL

image-20220902221048514

1.2 Basic concepts of STL

image-20220902221134122

1.3 Six major components of STL

image-20220902221329109

1.4 Containers, algorithms, and iterators in STL

image-20220902221547479

image-20220902221901324

1.5 First introduction to container algorithm iterators

image-20220903220433828

1.5.1 vector stores built-in data types

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 stores custom data types

Learning goal: Store custom data types in vector and print output

#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 container nested container

Learning goal: Nested containers within containers, we traverse and output all data

#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-common containers

2.1 string container

2.1.1Basic concepts of string

image-20221030213937104

2.1.2string constructor

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();
}

Summary: The various construction methods of string are not comparable. How to use them easily?

2.1.3 string assignment operation

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();

}

Summary: There are many ways to assign string values, and operator= is more practical.

2.1.4string string concatenation

image-20221101210127451

image-20221101210837929

2.1.5string search and replace

image-20221101211923701

image-20221101213228039

2.1.6 string string comparison

image-20221104204746500

image-20221104204834563

2.1.7 String character access

image-20221104205000666

image-20221104205113005

Modifications can be made in the same way

image-20221104205207629

2.1.8string insertion and deletion*

image-20221104205254720

image-20221104205759889

The insertion and deletion subscripts of str start from 0.

2.1.9string substring

image-20221104210132284

2.2vector container

2.2.1Basic concepts of vector

image-20221104210346981

image-20221104210407911

2.2.2vector constructor

image-20221104210506203

image-20221104211517965

image-20221104211527943

image-20221104211546778

image-20221104211603959

The various construction methods of vector are not comparable, just use them flexibly

2.2.3vector copy operation

image-20221104211717163

image-20221104212047501

image-20221104212125789

2.2.4 vetcor capacity and size

image-20221104212157953

image-20221104212527120

2.2.5 vetcor insertion and deletion

image-20221104212610398

image-20221104212856333

image-20221104212834864

2.2.6 Vector data access

image-20221104212931236

2.2.7 vector interchange container

image-20221104213053625

image-20221104213216717

Actual usage Shrink memory

image-20221104213347070

2.2.8 vector reserved space

image-20221104213506008

image-20221104213718955

2.3 deque container

2.3.1 Basic concepts of deque container

image-20221106205645632

image-20221106210409189

2.3.2 deque constructor

image-20221106210523672

image-20221106210741035

image-20221106210836929

2.3.3 deque assignment operation

image-20221106211006939

image-20221106211106396

2.3.4 deque size operation

image-20221106211345760

image-20221106211524943

2.3.5 deque insertion and deletion

image-20221106211722764

2.3.6 deque data access

image-20221106212035235

2.3.7 deque sorting

image-20221106212122015

image-20221106212305152

Summary: The sort algorithm is very practical. Just include the header file algorithm when using it.

2.4 stack container

2.4.1 Basic concepts of stack

image-20221106212437349

2.4.2 Common stack interfaces

image-20221106212604992

image-20221106212844832

2.5 queue container

2.5.1 Basic concepts of queue

image-20221106212956194

2.5.2 Queue common interfaces

image-20221106213057102

image-20221106222655752

2.6 list container

2.6.1 Basic concepts of list

image-20221106223152675

image-20221106223410850

image-20221106223446991

image-20221106223132829

2.6.2 list constructor

image-20221107083140522

image-20221107083335179

2.6.3 List assignment and exchange

image-20221107083410447

2.6.4 List size operation

image-20221107083637275

2.6.5 list insertion and deletion

image-20221107083829031

image-20221107084200916

2.6.6 List data access

image-20221107084406386

image-20221107084827538

2.7.7 List reversal and sorting

image-20221107084906925

image-20221107085207888

2.7.8 Sorting cases

image-20221107085233382

image-20221107085620044

image-20221107085700245

image-20221107085713865

2.7 set container

2.7.1 Basic concepts of set

image-20221107151922463

2.7.2 set container construction

image-20221107152206020

image-20221107152243596

2.7.3 Set size and exchange

image-20221107152311173

2.7.4 set insertion and deletion

image-20221107154224695

image-20221107154637982

2.7.5 set search and statistics

image-20221107154719862

2.7.6 The difference between set and multiset

image-20221107154904957

image-20221107155707207

image-20221107155734221

Summarize:

If inserting duplicate data is not allowed, you can use set

If you need to insert duplicate data, use multiset

2.7.7 pair creation

image-20221107160316031

image-20221107160650956

2.7.8 set container sorting

image-20221107160742746

Store built-in functions

image-20221107160947406

image-20221107161046043

Store custom functions

define type

image-20221107161506591

Modify built-in functions

image-20221107161542236

used when instantiating

image-20221107161616066

Summarize:

For custom data types, set must specify a collation before it can insert data.

2.8 map container

2.8.1 Basic concepts of map

image-20221107161719294

2.8.2 map construction and assignment

image-20221107162931163

image-20221107163606825

image-20221107163547290

Summary: All elements in the map appear in pairs, use pairs when inserting data

2.8.3 Map size and swapping

image-20221107164219864

2.8.4 map insertion and deletion

image-20221107164314292

image-20221107164448614

It is not recommended to use the fourth method. You can use key to access value.

image-20221107164608412

2.8.5 map statistics and search

image-20221107164657026

2.8.6 Map container sorting

image-20221107164743306

image-20221107164833611

Functor

image-20221107164900903

image-20221107164921988

2.9 Case-Employee Grouping

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-4JdS95jz-1667824623562) (C:/Users/62476/AppData/Roaming/Typora/typora-user-images/ image-20221107165840580.png)]

image-20221107165906843

3 stl-function object

3.1 Function formation

3.1.1 Function object concept

image-20221107195622425

3.1.2 Function object usage

image-20221107195925759

image-20221107200849040

image-20221107201033049

image-20221107201058846

3.2 Predicate

3.2.1 Predicate concept

image-20221107201246090

unary predicate

image-20221107201622400

image-20221107201641190

binary predicate

image-20221107201816233

image-20221107201958789

3.3 Built-in function objects

3.3.1 Meaning of traitor function object

image-20221107202820491

3.3.2 Arithmetic functors

image-20221107202913251

image-20221107203005403

3.3.3 Relational Functor

image-20221107203053039

image-20221107203203573

3.3.4 Logic Functor

image-20221107203342177

image-20221107203510846

Guess you like

Origin blog.csdn.net/charles_zhang_/article/details/127738791