[C++ to improve programming] STL first knowledge (2)

2 STL first acquaintance

2.1 The birth of STL

  • The software world has long wanted to build something that can be reused

  • The object-oriented and generic programming ideas of C++ are aimed at improving reusability

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

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

2.2 Basic Concepts of STL

  • STL (Standard Template Library, Standard Template Library )
  • STL is broadly divided into: container (container) algorithm (algorithm) iterator (iterator)
  • There is a seamless connection between containers and algorithms through iterators .
  • Almost all code in STL uses template classes or template functions

2.3 Six major 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 : acts as the glue between the container and the algorithm.
  4. Functor : A function that behaves like a function and can be used as a strategy for an algorithm.
  5. Adapter : A thing used to decorate a container or functor or iterator interface.
  6. Space Configurator : Responsible for space configuration and management.

2.4 Containers, Algorithms, and Iterators in STL

Containers : a place to keep things

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

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

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

​Sequence container : Emphasizes the ordering of values, and each element in the sequence container has a fixed position.
​Associative container : binary tree structure, there is no strict physical order relationship between elements

Algorithms : Solutions to Problems

A limited number of steps to solve a problem in logic or mathematics, this discipline we call algorithm (Algorithms)

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

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.

Invariant algorithm: It means that the content of the elements in the interval will not be changed during the operation, such as searching, counting, traversing, finding extreme values, etc.

Iterators : the glue between containers and algorithms

Provides a way to sequentially visit the elements contained in a container without exposing the container's internal representation.

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

Kind of iterator:

type Function support operation
input iterator read-only access to data Read only, supports ++, ==, ! =
output iterator write-only access to data write only, support ++
forward iterator Read and write operations, and can advance iterators 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 arbitrary data in a jumping manner, the most powerful iterator Read and write, support ++, –, [n], -n, <, <=, >, >=

Commonly used iterator types in containers are bidirectional iterators, and random access iterators

2.5 Introduction to Container Algorithm Iterators

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

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 Objective: Nested containers in containers, we will traverse all 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;
}

come on!

grateful!

effort!

Guess you like

Origin blog.csdn.net/qq_46092061/article/details/123619802