The 115th day learning record: C++ improvement: STL introduction (dark horse teaching video)

Birth of STL

1. For a long time, the software industry has always hoped to establish a reusable thing.
2. The object-oriented and generic programming ideas of C++ are aimed at improving repeatability.
3. In most cases, data structures and algorithms have not been consistent 4. In order
to establish a set of standards for data structures and algorithms, STL was born

Basic concepts of STL

1. STL, Standard Template Library
2. STL can be broadly divided into: container, algorithm, iterator
3. Containers and algorithms are seamlessly connected through iterators.
4. Almost all codes in STL use template classes or template functions.

STL six major components

STL is roughly divided into six major components, namely: container, algorithm, iterator, functor, adapter (adapter), space configurator 1,
container: various data structures, such as vector, list, deque, set, map etc., used to store data.
2. Algorithm: various commonly used algorithms, such as sort, find, copy, for_each, etc.
3. Iterator: acts as the glue between the container and the algorithm.
4. Functor: Behavior is similar to a function and can be used as a strategy for an algorithm.
5. Adapter: A thing used to modify a container or a functor or iterator interface.
6. Space configurator: responsible for space configuration and management.

Containers, Algorithms, Iterators in STL

**Container: **As the name suggests,
STL container is to realize some of the most widely used data structures.
Commonly used data structures: array, linked list, tree, stack, queue, collection, mapping table, etc.
These containers are sequential containers and associative containers. Two kinds of containers:
Sequential container: Emphasis on the ordering of values, each element in the sequential container has a fixed position
Associative container: Binary tree structure, there is no strict physical order relationship between elements

Algorithm:
limited steps to solve logical or mathematical problems. This discipline is called Algorithms. Algorithms are
divided into: qualitative change algorithm and non-qualitative change algorithm.
Qualitative change algorithm: refers to the content of elements in the interval that will be changed during the operation. Such as copying, replacing, deleting, etc.
Non-qualitative algorithm: It means that the content of elements in the interval will not be changed during the operation, such as searching, counting, traversing, finding extreme values, etc.

**Iterator: **The glue between the container and the algorithm
provides a method that enables it to sequentially search for each element contained in a container without exposing the internal expression of the container. Each container has its own dedicated iterator.
The use of iterators is very similar to pointers. At the beginning stage, we can first understand that iterators are pointers.

insert image description here
Iterators and pointers are concepts commonly used in programming, and they share some similarities, but also some notable differences.
Definition: A pointer is a variable used to store a memory address, which can point to any type of data. An iterator is an object or pointer used to traverse the elements in a container (such as an array, list, collection, etc.).
Function: The pointer can directly access and modify the data in the memory, and the flexible operation of the memory can be realized through pointer arithmetic. The iterator provides a unified way to access the elements in the container. You can access the elements in the container one by one in a certain order, and you can also perform operations such as addition, deletion, and modification.
Scope of use: Pointers are widely used in languages ​​such as C and C++ for low-level memory operations. Iterators are commonly used in high-level programming languages ​​such as C++ to provide a unified way to traverse and manipulate containers.
Security: The use of pointers requires attention to memory management issues, such as null pointers and wild pointers. The iterator is relatively safer, and it provides some methods to ensure traversal within the scope of the container and prevent out-of-bounds access.

container arithmetic iterator initialization

After understanding the concepts of containers, algorithms, and iterators in STL, we use code to feel the charm of STL. The
most commonly used container in STL is Vector, which can be understood as an array. Next, we will learn how to insert data into this container and traverse this container.

vector stores built-in data types

Container: vector
Algorithm: for_each

迭代器:vector<int>::iterator
#include<iostream>
using namespace std;
#include<vector>
#include<string>

void test01()
{
    
    
	//创建了一个vector容器,数组
	vector<int> v;
	vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
	vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器最后一个元素的下一个位置
	cout << sizeof(v) << endl;
	cout << sizeof(itBegin) << endl;
	cout << sizeof(itEnd) << endl;
	cout << sizeof(vector<int>) << endl;
	cout << sizeof(vector<string>) << endl;
	cout << sizeof(vector<int>::iterator) << endl;
	cout << sizeof(vector<string>::iterator) << endl;
}

int main()
{
    
    
	test01();
	return 0;
}

output:
insert image description here

insert image description here

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//第三种遍历方式用到:for_each 标准算法的头文件

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

	//第一种遍历方式(比较复杂)
	//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();
	return 0;
}

for_each() definition:

insert image description here

vector stores custom data types

#include<iostream>
using namespace std;
#include<vector>
#include<string>

//vector容器存放自定义数据类型
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容器
	vector<Person> v;
	
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中插入数据
	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;
		cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

//存放自定义数据类型  指针
void test02()
{
    
    
	//创建了一个vector容器
	vector<Person*> v;

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中插入数据
	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;
	}
}

int main()
{
    
    
	//test01();
	test02();
	return 0;
}

Vector container nested container

#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;

	//向小容器中添加数据
	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();
	return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45694614/article/details/131879262