当前位置:网站首页>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
边栏推荐
- Qt实战:云曦日历篇
- TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
- C语言p2选择分支语句详解
- Web page, adaptive, proportional scaling
- C语言知识点精细详解——初识C语言【1】
- Gif to still image processing
- Preview CSV file
- Usage of BC
- 一些小小小小记录~
- IE8 browser prompts whether to block access to JS script
猜你喜欢

Uni app message push

C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】

单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
![Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]](/img/1b/d1dea1726f3f91d2ba1ab8f2d7ff4c.jpg)
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]

关于在vs中使用scanf不安全的问题

流程控制之分支语句

TUN 设备原理

MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等

Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)

Qt界面优化:Qt去边框与窗体圆角化
随机推荐
翻牌效果
顺序栈的基本操作
x509证书cer格式转pem格式
OpenFaaS实战之四:模板操作(template)
js 抛物线运动方法封装
数组模拟队列进阶版本——环形队列(真正意义上的排队)
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
Redis源码分析之HSET流程与ziplist
ssh限制登录的四种手段
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
js 格式化时间
初始c语言大致框架适合复习和初步认识
Qt界面优化:鼠标双击特效
Detailed explanation of C language P2 selection branch statement
爬虫练习题(一)
XX project structure notes
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
gif转为静态图片处理
在电视屏幕上进行debug调试