Part 7 STL-Basics
1. First introduction to STL
1.1 The birth of STL
1.2 Basic concepts of STL
1.3 Six major components of STL
1.4 Containers, algorithms, and iterators in STL
1.5 First introduction to container algorithm iterators
1.5.1 vector stores built-in data types
#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
2.1.2string constructor
#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
#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
2.1.5string search and replace
2.1.6 string string comparison
2.1.7 String character access
Modifications can be made in the same way
2.1.8string insertion and deletion*
The insertion and deletion subscripts of str start from 0.
2.1.9string substring
2.2vector container
2.2.1Basic concepts of vector
2.2.2vector constructor
The various construction methods of vector are not comparable, just use them flexibly
2.2.3vector copy operation
2.2.4 vetcor capacity and size
2.2.5 vetcor insertion and deletion
2.2.6 Vector data access
2.2.7 vector interchange container
Actual usage Shrink memory
2.2.8 vector reserved space
2.3 deque container
2.3.1 Basic concepts of deque container
2.3.2 deque constructor
2.3.3 deque assignment operation
2.3.4 deque size operation
2.3.5 deque insertion and deletion
2.3.6 deque data access
2.3.7 deque sorting
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
2.4.2 Common stack interfaces
2.5 queue container
2.5.1 Basic concepts of queue
2.5.2 Queue common interfaces
2.6 list container
2.6.1 Basic concepts of list
2.6.2 list constructor
2.6.3 List assignment and exchange
2.6.4 List size operation
2.6.5 list insertion and deletion
2.6.6 List data access
2.7.7 List reversal and sorting
2.7.8 Sorting cases
2.7 set container
2.7.1 Basic concepts of set
2.7.2 set container construction
2.7.3 Set size and exchange
2.7.4 set insertion and deletion
2.7.5 set search and statistics
2.7.6 The difference between set and multiset
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
2.7.8 set container sorting
Store built-in functions
Store custom functions
define type
Modify built-in functions
used when instantiating
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
2.8.2 map construction and assignment
Summary: All elements in the map appear in pairs, use pairs when inserting data
2.8.3 Map size and swapping
2.8.4 map insertion and deletion
It is not recommended to use the fourth method. You can use key to access value.
2.8.5 map statistics and search
2.8.6 Map container sorting
Functor
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)]
3 stl-function object
3.1 Function formation
3.1.1 Function object concept
3.1.2 Function object usage
3.2 Predicate
3.2.1 Predicate concept
unary predicate
binary predicate
3.3 Built-in function objects
3.3.1 Meaning of traitor function object
3.3.2 Arithmetic functors
3.3.3 Relational Functor
3.3.4 Logic Functor