C++ Xiaobai's STL first acquaintance

C++ Xiaobai's STL first acquaintance

Not much to say, the daily tidying up time is up. What I want to tidy up today is the STL (standard template library). I have learned a little bit about it, and I think STL is really easy to use o(  ̄▽ ̄ )ブ. Today is my first acquaintance with STL, so let’s summarize it.

1. STL initial-the basic concept of
STL 1.1 The birth of STL
(1) For a long time, the software industry has been hoping to build a reusable thing.
(2) C++'s object-oriented and generic programming ideas are aimed at improving reusability.
(3) 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.
(4) In order to establish a set of standards for data structures and algorithms, STL was born.

1.2 STL basic concepts
(1) STL (standard template library, standard template library)
(2) STL can be broadly divided into: container (container), algorithm (algorithm), iterator (iterator)
(3) between container and algorithm Seamless connection through iterators
(4) Almost all STL codes use template classes or template functions

1.3 The six components of
STL STL is roughly divided into six components, namely: container, algorithm, iterator, functor, adapter (adapter), space adapter
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: It acts as the glue between the container and the algorithm.
4. Functors: similar functions can be used as a strategy for algorithms.
5. Adapter: a thing used to decorate the interface of a container or functor or iterator.
6. Space adapter: responsible for space configuration and management.

1.4 Containers, algorithms, and iterators in STL
Containers: the place of storage.
STL containers implement the most widely used data structure.
Commonly used data structures: arrays, linked lists, numbers, stacks, queues, sets, mapping tables, etc.
These containers are divided into two types: sequence container and associative container:
sequence container: emphasizes the ordering of values, each element in the sequence container has a fixed position.
Associative container: Binary tree structure, there is no strict physical order relationship between the elements.

Algorithms: The problem-solving method also has
limited steps to solve logical or mathematical problems. We call this subject algorithm (Algorithms).
Algorithms are divided into qualitative and non-qualitative algorithms.
Qualitative change algorithm: It means that the content of the elements in the interval will be changed during the operation, such as copying, replacing, deleting, 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, and so on.

Iterator: The glue between the container and the algorithm
Provides a way to sequentially search for the 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. In the initial stage, you can understand that iterators are pointers.
Types of
iterators : input iterators
Insert picture description here
Commonly used types of iterators in containers are bidirectional iterators and random access iterators.

1.5 vector stores built-in data types
Container: vector
algorithm: for_each
iterator: vector:: iterator
STL algorithm header file: #include <algorithm>
three traversal iterators and test code:

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

void myPrint(int val)
{
    
    
cout<<val<<endl;
}

void test01()
{
    
    
//创建了一个vector容器,数组
	vector<int> v;

//向容器中插入数据
	v.push_back(10);
	v.push_back(11);
	v.push_back(12);
	v.push_back(13);
	v.push_back(14);

//通过迭代器访问容器中的数据

	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);//需要在声明一个myPrint的函数

}

int main()
{
    
    
	test01();

system("pause");
return 0;
}

1.6 The demonstration code for storing custom data types in vector is
as follows:

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

Person p1("wjx",11);
Person p2("p",11);
Person p3("苏",10);
Person p4("re",11);
Person p5("tyu",11);

//向容器中添加数据
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<Person*>v;

Person p1("wjx",11);
Person p2("py",11);
Person p3("苏",10);
Person p4("re",11);
Person p5("tyu",11);

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

system("pause");
return 0;
}

1.7 Container nested container
Similar to two-dimensional array call, the usage code is as follows:
divided into a total large container, and a small container contained in the large 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();


system("pause");
return 0;
}

Guess you like

Origin blog.csdn.net/qq_45252077/article/details/107915940