当前位置:网站首页>First acquaintance with STL
First acquaintance with STL
2022-04-23 14:28:00 【Spiritual health】
First time to know STL
STL brief introduction :
STL(Standard Template Library), Standard template library , It's an industrial strength , efficient C++ library . Is a set of powerful C++ Template class , Provides generic template classes and functions , These template classes and functions can implement a variety of popular and commonly used algorithms and data structures , Like a vector 、 Linked list 、 queue 、 Stack .
C++ The core of the standard template library includes containers (Containers)、 Algorithm (Algorithms)、 iterator (iterators) Three components , among 1. A container is a collection of objects of a certain type ,C++ There are different types of containers , such as deque、list、vector、map etc. ;2. The algorithm works on the container . They provide ways to perform various operations , Includes initializing the contents of the container 、 Sort 、 Search and conversion operations ;3. Iterators are used to traverse the elements of an object collection . These collections can be containers , It could also be a subset of the container . All three components come with rich predefined functions , Help us deal with complex tasks in a simple way .
STL Characteristics :
STL One of the most important features of is the separation of data structure and algorithm :
Although it's a simple concept , But this separation does make STL Become very versatile . for example , because STL Of sort() Functions are completely generic , You can use it to manipulate almost any set of data , Including the list , Containers and arrays ;
STL Another important feature is that it is not object-oriented :
In order to be universal enough ,STL Mainly depends on templates rather than encapsulation , Inheritance and polymorphism ——OOP The three elements of . you are here STL You can't find any obvious class inheritance relationships in . It's like a kind of retrogression , But this is what makes STL The components have the underlying characteristics of wide commonality . in addition , because STL It's based on templates , The use of inline functions makes the generated code short and efficient ;
STL Components :
STL Components It mainly includes Containers , iterator 、 Algorithms and affine functions .
Containers
Containers are used to store and manage a collection of objects of a certain kind .
For example, a fish tank is a container for goldfish . Every container has its advantages and disadvantages . To meet the needs of the program ,STL A variety of container types are available , The container can be arrays or linked lists, Or each element has a special key value .
iterator
Iterators are used to traverse elements of an object cluster . Object clusters can be containers , It could be part of the container .
The main purpose of iterators is to provide a small set of common interfaces for containers . Use this interface , An operation can progress to the next element in the cluster . Each container provides its own iterator . The iterator understands the internal structure of the container , So it's going to go right . The interface of an iterator is similar to a general pointer .
Algorithm
Used to handle elements in the cluster , You can search for... For different purposes 、 Sort 、 modify 、 Use those elements .
All container iterators provide a consistent interface , With the help of iterators , Algorithmic programs can be used in any container .
STL One of the features of is the separation of data and operations . Data is managed by container categories , Operations are defined by customizable algorithms . An iterator acts in between “ adhesive ”, So that the algorithm can interact with the container .
STL Another feature of is that components can operate against any type .“ Standard template library ” The name means “ Any type is acceptable ” The template of , And these types can perform the necessary operations .
stay STL in , Containers are divided into Sequential containers and Associative containers Two categories: , The function of iterator is to traverse all or part of the objects in the container . Iterators can be divided into 5 Species and genera , this 5 The species belong to two types : Bidirectional iterators and random access iterators .
SIL The algorithms provided in include searching for 、 Sort 、 Copy 、 reorder 、 modify 、 Numerical operation, etc .
functor
STL Imitation functions are widely used in . Imitation function has the powerful power of generic programming , Is an example of a purely abstract concept .
STL Containers and algorithms :
Containers
STL Contains many container classes . A container class is a class that can contain other objects , Like arrays and queue stacks, data structures contain integers 、 decimal 、 Class and other data members .STL Can contain common vector classes 、 List class 、 Two way queue class 、 Collection classes 、 Graph class, etc , Each class is a template , These templates can contain various types of objects .
The following code is commonly used vector Assignment method :
vector <int> l;
for (int i =0;i <100;i++ )
l.push_back (i);
The following code uses map The container manages two-dimensional elements :
map <string, string, less <string>> cap; // Sort from small to large
cap ["Ark"] ="Little Rock";
cap ["New"] ="Albany";
map Be similar to Two dimensional array , But it's much more flexible than a two-dimensional array .
at present ,STL The containers already provided in are mainly as follows :
- vector : A vector .
- list : A two-way linked list container , Completed the standard C++ All functions of linked list in data structure .
- queue : A queue container , Completed the standard C++ All the functions of the queue in the data structure .
- stack : A stack container , Completed the standard C++ All functions of the stack in the data structure .
- deque : Double ended queue container , Completed the standard C++ All functions of the stack in the data structure .
- priority_queue : A queue container sorted by value .
- set : A collection container .
- multiset : A collection container that allows duplicate elements to occur .
- map <key, val>: An associative array container .
- multimap <key, val>: One that allows repetition key Value's associative array container .
The above container design is efficient , The interface is also provided . Programmers can use them wherever appropriate .
Containers Can be divided into Sequential containers and Associative containers Two categories: . Sequential containers There are mainly vector、list and deque; Associative containers Include set、map、multiset and multimap Container template class .
Algorithm
STL Provides a lot of data structure algorithms . These algorithms are in the namespace std Within the scope of , By including header files <algorithm> To get the right to use .
Some common algorithms are as follows :
- for_each();
- find();
- find_if();
- count();
- count_if();
- replace();
- replace_if();
- copy();
- unique_copy();
- sort();
- equal_range();
- merge();
STL All algorithms in are implemented based on templates .
Out of curiosity , From browsing the Internet without permission , No systematic , Overall learning , If there are any inaccuracies and deficiencies, please give advice ψ(`∇´)ψ( ̄︶ ̄)
版权声明
本文为[Spiritual health]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411422961.html
边栏推荐
- 一些小小小小记录~
- 逻辑卷创建与扩容
- 51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
- XX project structure notes
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- Usage of BC
- LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
- 电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
- Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
- 网页自适应,等比缩放
猜你喜欢
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
uni-app消息推送
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
Use the executors class to quickly create a thread pool
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
如何5分钟上手使用OCR
关于UDP接收icmp端口不可达(port unreachable)
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
随机推荐
bc的用法
The initial C language framework is suitable for review and preliminary understanding
flannel 原理 之 TUN模式
Nacos uses demo as configuration center (IV)
tcp_diag 内核相关实现 1 调用层次
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Flop effect
OpenFaaS实战之四:模板操作(template)
网页自适应,等比缩放
Use cases of the arrays class
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
Four ways of SSH restricting login
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
Detailed explanation of C language P2 selection branch statement
JS recursion (1)
Five ways of using synchronized to remove clouds and fog are introduced
Redis源码分析之HSET流程与ziplist
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code