C++ study notes 23-STL introduction and simple and practical vector

22.1 Preface - The Birth of STL

  • For a long time, the software world has wanted to build something reusable.
  • The purpose of C++'s object-oriented and generic programming ideas is to improve reusability .
  • In most cases, data structures and algorithms have failed to have a set of standards, resulting in a lot of duplication of work.
  • In order to establish a set of standards for data structures and algorithms, STL was born .

22.2 Basic Concepts of STL

  • STL (standard Template Library, standard template library)
  • STL is broadly divided into: container (container), algorithm (algorithm), iterator (iterator)
  • The container and algorithm are seamlessly connected through iterators (iterators are equivalent to bridges).
  • Almost all code in the STL uses template classes or template functions.

22.3 Six components of STL

STL is roughly divided into six major components, namely: container, algorithm, iterator, functor, adapter (adapter), and space configurator.

  1. Container: Various data structures, such as vector, list, deque, set, map, etc., are used to store data.
  2. Algorithms: 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: Behaves like a function and can be used as some kind of strategy for an algorithm.
  5. Adapter: A thing used to decorate a container or a functor or iterator interface.
  6. Space configurator: Responsible for space configuration and management.

22.4 Containers, Algorithms, Iterators in STL

22.4.1 Containers

Container: a storage place.
The STL container is to realize some of the most widely used data structures .
Commonly used data structures: arrays, linked lists, trees, stacks, queues, sets, mapping tables, etc.
These containers can be divided into two types: sequential containers and associative containers:

  • Sequential container: Emphasizes the sorting of values, and each element in the sequential container has a fixed position (all the ones I have learned so far are sequential).
  • Associative container: Binary tree structure, there is no strict physical order relationship between elements.

22.4.2 Algorithms

Algorithms: The solution to the problem is also
limited steps to solve logical or mathematical problems. This subject is called Algorithms.
Algorithms are divided into: qualitative change algorithms and non-qualitative change algorithms .

  • Qualitative change algorithm: It means that the content of the elements in the interval will be changed during the operation. such as copy, replace, delete, 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.

22.4.3 Iterators

Iterators: The Glue Between Containers and Algorithms
Provide a way to sequentially seek through the elements contained in a container without exposing the container's internal representation. Algorithms use iterators to access the elements in 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.

Types of iterators:
insert image description hereCommonly used types of iterators in containers are bidirectional iterators and random access iterators


22.5 Introduction to Container Algorithm Iterators

22.5.0 Preface

After understanding the concepts of containers, algorithms, and iterators in STL. We use the 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.

22.5.1 Vector stores built-in data types

Container: vector, need to import header file <vector>
Algorithm: for_each, need to import standard algorithm header file <algorithm>
Iterator:vector<int>::iterator

Example:

#include<iostream>
using namespace std;
#include<vector>  //STL中所有东西都要头文件
#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);

	//通过迭代器来访问容器中的数据
	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);
	//for_each作用就是依次将数据取出防止自己定义的函数中
}

int main()
{
    
    	
	test01();
	system("pause");
	return 0;
}

22.5.2 Vector stores custom data types

Example:

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>
class Person2
{
    
    
public:
	Person2(string name, int age)
	{
    
    
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};
//存放自定义数据类型
void test2_01()
{
    
    
	vector<Person2> v;
	Person2 p1("aaa", 10);
	Person2 p2("bbb", 20);
	Person2 p3("ccc", 30);
	Person2 p4("ddd", 40);
	Person2 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<Person2>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << "姓名: " << (*it).name << " 年龄: " << (*it).age << endl;
		//cout << "姓名: " << it->name << " 年龄: " << it->age << endl;
	}

}
//存放自定义数据类型的指针
void printPerson2(Person2* p)
{
    
    
	cout << "姓名: " << p->name << " 年龄: " << p->age << endl;
}
void test2_02()
{
    
    
	vector<Person2*> v;
	Person2 p1("aaa", 10);
	Person2 p2("bbb", 20);
	Person2 p3("ccc", 30);
	Person2 p4("ddd", 40);
	Person2 p5("eee", 50);

	//添加
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);

	//遍历
	cout << "-------------------" << endl;
	for_each(v.begin(), v.end(), printPerson2);
}

int main()
{
    
    
	test2_01();
	test2_02();
	system("pause");
	return 0;
}

22.5.3 Vector container nested container

Example:

#include<iostream>
using namespace std;
#include<vector>
void test3_01()
{
    
    
	vector<vector<int>> v;

	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	vector<int>v4;

	for (int i = 0; i < 5; i++)
	{
    
    
		v1.push_back(i);
		v2.push_back(i+1);
		v3.push_back(i+2);
		v4.push_back(i+3);
	}

	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++)
	{
    
    
		for (vector<int>::iterator jit = it->begin(); jit != it->end(); jit++)
		{
    
    
			cout << *jit << " ";
		}
		cout << endl;
	}
}

int main()
{
    
    
	test3_01();
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_49030008/article/details/123648299