[C++ to improve programming] 2. STL first acquaintance

2 STL first encounter

2.1 The birth of STL

  • For a long time, the software industry has always wanted to build a reusable thing

  • C++'s object-oriented and generic programming ideas are aimed at improving reusability

  • In most cases, data structures and algorithms fail to have a set of standards, resulting in forced to engage in a lot of repetitive work

  • In order to establish a set of standards for data structures and algorithms, STL was born

2.2 STL basic concepts

  • STL (Standard Template Library, Standard Template Library )
  • STL can be broadly divided into: container (container) algorithm (algorithm) iterator (iterator)
  • The container and the algorithm are seamlessly connected through iterators .
  • Almost all STL code uses template classes or template functions

2.3 Six Components of STL

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., are used to store data.
  2. Algorithms: various commonly used algorithms, such as sort, find, copy, for_each, etc.
  3. Iterator: It acts as the glue between the container and the algorithm.
  4. Functor: behaves like a function, which can be used as a certain strategy of the algorithm.
  5. Adapter: a thing used to decorate the interface of a container or functor or iterator.
  6. Space Configurator: Responsible for space configuration and management.

2.4 Containers, algorithms, and iterators in STL

**Container: **A place to store things

STL container is to realize some of the most widely used data structures

Commonly used data structures: arrays, linked lists, trees, stacks, queues, collections, mapping tables, etc.

These containers are divided into two types: sequential containers and associative containers :

Sequence containers : emphasized sorting sequence of each container element values have a fixed position. Associative containers : a binary tree, there is no strict sequential relationship between the physical elements

**Algorithm: **The solution to the problem is also

A limited number of steps to solve logical or mathematical problems, this subject is called algorithms (Algorithms)

Algorithms are divided into: qualitative change algorithm and non-qualitative change algorithm .

Qualitative change algorithm: refers to the content of the elements in the interval that will be changed during the operation. Such as copy, replace, delete, etc.

Non-qualitative algorithm: It means that the element content 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

Provide a way to sequentially search for the various elements contained in a container without exposing the internal representation of the container.

Each container has its own 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:

species Features Support calculation
Input iterator Read-only access to data Read only, support ++, ==,! =
Output iterator Write-only access to data Write only, support++
Forward iterator Read and write operations, and can advance the iterator Read and write, support ++, ==,! =
Bidirectional iterator Read and write operations, and can operate forward and backward Read and write, support ++, –,
Random access iterator Read and write operations, you can access any data in a jump mode, the most powerful iterator Read and write, support ++, –, [n], -n, <, <=, >, >=

The types of iterators in commonly used containers are bidirectional iterators and random access iterators

2.5 A first look at container algorithm iterators

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. Below we will learn how to insert data into this container and traverse this container

2.5.1 vector stores built-in data types

container: vector

algorithm: for_each

Iterator: vector<int>::iterator

Example:

#include <vector>
#include <algorithm>

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.begin()返回迭代器,这个迭代器指向容器中第一个数据
	//v.end()返回迭代器,这个迭代器指向容器元素的最后一个元素的下一个位置
	//vector<int>::iterator 拿到vector<int>这种容器的迭代器类型

	vector<int>::iterator pBegin = v.begin();
	vector<int>::iterator pEnd = v.end();

	//第一种遍历方式:
	while (pBegin != pEnd) {
		cout << *pBegin << endl;
		pBegin++;
	}

	
	//第二种遍历方式:
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << endl;
	}
	cout << endl;

	//第三种遍历方式:
	//使用STL提供标准遍历算法  头文件 algorithm
	for_each(v.begin(), v.end(), MyPrint);
}

int main() {

	test01();

	system("pause");

	return 0;
}

2.5.2 Vector stores custom data types

Learning objective: store custom data types in vector and print out

Example:

#include <vector>
#include <string>

//自定义数据类型
class Person {
public:
	Person(string name, int age) {
		mName = name;
		mAge = age;
	}
public:
	string mName;
	int mAge;
};
//存放对象
void test01() {

	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 << "Name:" << (*it).mName << " Age:" << (*it).mAge << endl;

	}
}


//放对象指针
void test02() {

	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++) {
		Person * p = (*it);
		cout << "Name:" << p->mName << " Age:" << (*it)->mAge << endl;
	}
}


int main() {

	test01();
    
	test02();

	system("pause");

	return 0;
}

2.5.3 Vector container nested container

Learning goal: nested containers in containers, we will traverse all the data and output

Example:

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

	//将容器元素插入到vector v中
	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 vit = (*it).begin(); vit != (*it).end(); vit++) {
			cout << *vit << " ";
		}
		cout << endl;
	}

}

int main() {

	test01();

	system("pause");

	return 0;
}

For more information, please follow the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/114442104