stl标准模板库_C ++标准模板库(STL)–简介

stl标准模板库

In this tutorial you will learn about what is STL in C++, what it provides and overview of all STL items.

在本教程中,您将了解C ++中的STL,它提供了什么以及所有STL项目的概述。

STL means Standard Template Library. STL is the most crafted libraries among all other libraries in C++. It is heart of C++. It includes containers, algorithms and iterators.

STL表示标准模板库 。 STL是C ++中所有其他库中最精心设计的库。 这是C ++的心脏。 它包括容器,算法和迭代器。

C ++标准模板库(STL) (C++ Standard Template Library (STL))

What STL Provides?

STL提供什么?

Standard Template Library provides Algorithms and Containers. Containers contain data and algorithms operate on the data which is in containers.

标准模板库提供算法和容器 。 容器包含数据,并且算法对容器中的数据进行操作。

The aim of object oriented language is combining algorithm and data into a class which is called object. STL goes opposite way of OOP language. It separates algorithms and data structures. Purpose of that is code reuse. We want one algorithm apply on different containers. And we want one container to use different algorithms. Containers have different interfaces from each other. So if we want to apply one algorithm on different containers, we need to provide different implementations for that. So if we have N algorithms and M containers we need to provide N*M implementations. This is quite lot work and not scalable also.

面向对象语言的目的是将算法和数据组合到一个称为对象的类中。 STL与OOP语言相反。 它分离算法和数据结构。 这样做的目的是代码重用。 我们希望一种算法适用于不同的容器。 我们希望一个容器使用不同的算法。 容器具有彼此不同的接口。 因此,如果我们想将一种算法应用于不同的容器,则需要为此提供不同的实现。 因此,如果我们有N个算法和M个容器,则需要提供N * M个实现。 这是一项很大的工作,并且也无法扩展。

To solve this problem STL library provides another group of modules called Iterators. Each container required to provide a common interface defined by iterators. Iterator can iterate each item inside a container. So the algorithm instead of working on containers directly it only works on the iterators. So the algorithm doesn’t know about on which container it is working. It only knows about the iterator. This will very useful to reuse the code instead of writing N*M implementations (above mentioned example). Now we only need to provide N+M implementations. There are so many uses by this. If we define a new algorithm that operates on iterator then all the existing containers can use that algorithm. Similarly if we define a new container that provides appropriate iterate interface then all the existing algorithms can be applied on that new container. So this ST Library is very much useful.

为了解决此问题,STL库提供了另一组称为Iterators的模块。 每个容器都需要提供由迭代器定义的公共接口。 迭代器可以迭代容器内的每个项目。 因此,该算法不是直接在容器上运行,而是仅在迭代器上运行。 因此,算法不知道它在哪个容器上工作。 它只知道迭代器。 这对于重用代码而不是编写N * M实现非常有用(上述示例)。 现在,我们只需要提供N + M实现即可。 这样有很多用途。 如果我们定义了一种在迭代器上运行的新算法,则所有现有容器都可以使用该算法。 同样,如果我们定义一个提供适当的迭代接口的新容器,则所有现有算法都可以应用于该新容器。 因此,此ST库非常有用。

C++ Standard Template Library (STL)

Image Source

图片来源

货柜 (Containers)

序列容器 (Sequence Containers)

These implemented with arrays and linked lists.

这些通过数组和链表实现。

  • Vector: Vector is a dynamic array that grows in one direction; it is at end of vector.

    向量向量是一个在一个方向上增长的动态数组。 它在向量的末尾。

  • Deque: Deque is similar to vector. But deque can grow both beginning and at ending in both directions

    双端队列(Deque) :双端队列类似于vector。 但是双端队列在两个方向的开始和结束都可能增长

  • List: It is same as double linked list. Each item in list points to both previous and next item of that item.

    列表与双链表相同。 列表中的每个项目都指向该项目的上一个和下一个项目。

  • Forward list: Forward list only contain forward pointer. So it can traverse beginning to the end but not from end to the beginning.

    转发列表转发列表仅包含转发指针。 因此它可以从头到尾遍历,但不能从头到尾遍历。

  • Array: There are limitations to container array. That is size can’t be changed.

    数组容器数组有一些限制。 那是大小不能改变。

关联容器 (Associative Containers)

These are typically implemented by Binary Trees. The key attribute of associative containers is all the elements are always sorted.

这些通常由二叉树实现。 关联容器的关键属性是始终对所有元素进行排序。

  • Set: Set has no duplicate items. When we insert elements into set all elements automatically sorted. It takes O(log( n)) time.

    集合集合没有重复的项目。 当我们将元素插入集合时,所有元素都会自动排序。 这需要O(log(n))时间。

  • Multiset: It is same as set. But only difference is it allows duplicates also.

    多重设置与设置相同。 但是唯一的区别是它也允许重复。

  • Map: Sometimes we don’t want to sort items by values. We are interested to sort them by key value. Map and Multimap contain key value pair structure. Map doesn’t allow duplicate keys.

    地图有时我们不想按值对项目进行排序。 我们有兴趣按键值对它们进行排序。 Map和Multimap包含键值对结构。 地图不允许重复的键。

  • Multimap: It is just like a map. But only difference is it allows duplicate keys. Important note is that, keys can’t be modified in map and multimap.

    Multimap就像一张地图。 但是唯一的区别是它允许重复的密钥。 重要说明是,键不能在map和multimap中修改。

The word associative means, associating key with value. We can say that, set and multiset are special kind or map or multimap where the key of element is the same. This is the reason they called associative.

单词associative意味着将键与值相关联。 可以说,set和multiset是特殊的种类或map或multimap,其中元素的键相同。 这就是他们称之为关联的原因。

无序集装箱 (Unordered Containers)

Unordered containers internally implemented with hash table. Each item calculated by hash function, to map to hash table. The main advantage is if we have effective hash function we can find elements in O (1) time.

内部使用哈希表实现的无序容器。 每个项目由哈希函数计算,以映射到哈希表。 主要优点是,如果我们具有有效的哈希函数,我们可以在O(1)时间中找到元素。

This is fastest among all containers.

在所有容器中,这是最快的。

In Unordered containers the order is not defined. And they may change over the time.

在无序容器中,未定义顺序。 随着时间的流逝,它们可能会发生变化。

容器适配器 (Container Adaptors)

Along with containers, container adaptors also provided by the ST library.

除容器外,ST库还提供了容器适配器。

Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes (such as deque or list) to handle the elements. The underlying container is encapsulated in such a way that its elements are accessed by the members of the container adaptor independently of the underlying container class used.

容器适配器不是完整的容器类,而是提供特定接口的类,这些接口依赖于容器类之一(例如双端队列或列表)的对象来处理元素。 基础容器的封装方式使得容器适配器的成员可以独立于所使用的基础容器类来访问其元素。

  • Stack: Stack provides push, pop, top operations to work on it

    堆栈堆栈提供推入,弹出,顶部操作以对其进行处理

  • Queue: Push, pop, front, back allowed operations on this.

    队列对此进行推,弹出,向前,向后允许的操作。

  • Priority Queue: It is keys of items of different priority. First item always have the highest priority. 

    优先级队列它是具有不同优先级的项目的键。 第一项始终具有最高优先级。  

迭代器 (Iterators)

There are five categories of iterators based on types of operations performed on them.

根据对迭代器执行的操作类型,可以将其分为五类。

1. Random Access Iterator

1.随机访问迭代器

In random access iterator we can access elements in a container randomly. Here we can increment, decrement, add, subtract and also can compare. We can perform all these operations.

在随机访问迭代器中,我们可以随机访问容器中的元素。 在这里我们可以增加,减少,增加,减去,也可以进行比较。 我们可以执行所有这些操作。

Ex: Vector, deque, Array provides random access iterator.

例如:向量,双端队列,数组提供随机访问迭代器。

2. Bidirectional Iterator

2.双向迭代器

With bidirectional iterator we can increment and decrement but we can’t add or subtract or compare.

使用双向迭代器,我们可以递增和递减,但不能加,减或比较。

Ex: list , set/multiset, map/multimap provide bidirectional iterators.

例如:list,set / multiset,map / multimap提供了双向迭代器。

3. Forward Iterator

3.转发迭代器

Forward iterator can only be incremented can’t be decremented.

前向迭代器只能递增,不能递减。

Ex: forword_list

例如:forword_list

Remaining “Un ordered containers” they provide at least “forward iterators”. And also chance to growth “bidirectional iterators”. It depends on implementation of the STL.

剩下的“未排序的容器”至少提供了“转发迭代器”。 并有机会发展“双向迭代器”。 这取决于STL的实现。

4. Input Iterator

4.输入迭代器

Input iterator used to read and process values while iterating forward. We can read only but not write.

输入迭代器,用于在向前迭代时读取和处理值。 我们只能读而不能写。

5. Output Iterator

5.输出迭代器

Used to output values.  We can write but not read. Both input iterator and output iterator can only move forward. They can’t move backward.

用于输出值。 我们可以写但不能读。 输入迭代器和输出迭代器都只能向前移动。 他们不能向后移动。

Iterator Adaptor (Predefined Iterator)

迭代器适配器(预定义的迭代器)

This is a special, more powerful iterator.

这是一个特殊的,功能更强大的迭代器。

  • Insert iterator

    插入迭代器
  • Stream iterator

    流迭代器
  • Reverse iterator

    反向迭代器
  • Move iterator

    移动迭代器

演算法 (Algorithms)

Algorithms are mostly loops. Algorithms always process ranges in a half-open way. That means it will include the first item but not include the last item.

算法主要是循环。 算法始终以半开放方式处理范围。 这意味着它将包括第一项,但不包括最后一项。

非修改算法 (Non-modifying Algorithms)
  • Count: Count function counts number of items in some data range.

    计数:计数功能对某个数据范围内的项目数进行计数。

  • Min and max: Max elements returns first maximum number, min returns minimum number.

    最小值和最大值:最大值元素返回第一个最大值,最小值返回最小值。

  • Linear Search: When data is not sorted, this search for an element and returns first match.

    线性搜索:不对数据进行排序时,此搜索将搜索元素并返回第一个匹配项。

  • Binary Search: When data is sorted, this search for an element and returns first match

    二进制搜索:对数据进行排序时,此搜索将搜索元素并返回第一个匹配项

  • Comparing ranges: Used to compare between two vector/list….etc

    比较范围:用于比较两个向量/列表….etc

  • Check attributes: There are different check attributes in this like is sorted, any of/none of satisfied given condition… etc… We learn all of these clearly in further modules.

    检查属性:有不同的检查属性,例如已排序,任何/没有满足的给定条件……等等……我们将在进一步的模块中清楚地学习所有这些。

修改算法(值更改算法) (Modifying Algorithms (Value Changing Algorithms))
  • Copy: Copies everything from one container to other.

    复制:将所有内容从一个容器复制到另一个容器。

  • Move: It moves items to one place to other.

    移动:将项目移动到另一个地方。

  • Transform: It transforms the data with certain operation. And then save the results at different place.

    转换:通过一定的操作转换数据。 然后将结果保存在其他位置。

  • Swap: Swap the data between two places. It is also called two way copying.

    交换:在两个位置之间交换数据。 也称为双向复制。

  • Fill: Fill is used to fill the data with certain values.

    填充:填充用于用某些值填充数据。

  • Replace: Replace the existing value in container with another value.

    替换:用另一个值替换容器中的现有值。

  • Remove: Remove can remove any element based on condition.

    删除:删除可以根据条件删除任何元素。

订单更改算法 (Order Changing Algorithms)

They changes the order of the elements in container, but not necessarily the elements themselves.

它们更改容器中元素的顺序,但不一定更改元素本身。

  • Reverse: Reverse the entire data.

    反转:反转整个数据。

  • Rotate: Rotates the data from vector begin to vector end.

    旋转:将数据从矢量开始旋转到矢量结束。

  • Permute: Next permutation results lexicographically next greater permutation. Prev permutation results lexicographically next small permutation.

    置换:按照字典顺序,下一个置换产生下一个更大的置换。 上一个排列结果按字典顺序显示下一个小排列。

  • Shuffle: Rearrange the elements randomly. Swap each element with a randomly selected element.

    随机播放:随机重新排列元素。 用随机选择的元素交换每个元素。

排序算法 (Sorting Algorithms)

Sorting algorithms are widely used algorithms in programming.

排序算法是编程中广泛使用的算法。

Sorting algorithms requires random access iterators. So that option limits our options to containers vector, deque, container array, native array. List and un-ordered containers cannot be sorted. And associative container doesn’t need sort anyway.

排序算法需要随机访问迭代器。 因此,该选项将我们的选项限制为容器向量,双端队列,容器数组,本机数组。 列表容器和无序容器无法排序。 而且关联容器也不需要排序。

Sort() function directly sort by default. Some other cases also in this. We learn those in further tutorials.

默认情况下, Sort()函数直接排序。 在这种情况下也有其他情况。 我们将在进一步的教程中学习这些内容。

Partial sort: Sometimes we doesn’t need all elements need to be sorted. In that case we use this partial sort like top 5 elements or least 10 elements like this.

部分排序:有时候我们不需要对所有元素进行排序。 在这种情况下,我们使用这种部分排序,例如前5个元素或至少10个元素。

堆算法 (Heap Algorithms)

Heap has two properties: First element is always larger and add/remove takes O(log(n)) time.

堆具有两个属性:第一个元素始终较大,添加/删除操作需要O(log(n))时间。

Heap algorithms often used to implement priority queue. These are four important heap algorithms.

堆算法通常用于实现优先级队列。 这是四个重要的堆算法。

  • Make_heap: Creates new heap on given container.

    Make_heap:在给定的容器上创建新的堆。

  • Pop_heap: It removes largest element from heap, for that it does two things, swaps the first and last items of the heap and then heapify to satisfy heap conditions.

    Pop_heap:它从堆中删除最大的元素,因为它做两件事,交换堆的第一项和最后一项,然后进行堆化以满足堆条件。

  • Push_heap: Add new element into heap.

    Push_heap:将新元素添加到堆中。

  • Sort_heap: It does heap sorting on given heap.

    Sort_heap:在给定的堆上进行堆排序。

排序数据算法 (Sorted Data Algorithms)

 These are the algorithms that require data being pre-sorted.

  这些是需要对数据进行预排序的算法。

  • Binary search: It checks for data in a data range. It returns a Boolean as a result.

    二进制搜索:它检查数据范围内的数据。 结果返回一个布尔值。

  • Merge: Merge operation does two ranges of sorted data into one range of big sorted data.

    合并:合并操作会将两个范围的已排序数据转换为一个范围较大的排序数据。

  • Set operations: Set Union, Set intersection, Set difference, Symmetric difference all these are set operations.

    设置操作:设置并集,设置相交,设置差,对称差所有这些都是设置操作。

数值算法 (Numeric Algorithms)

Numeric algorithms defined in the header of numeric not in the header of algorithm.

在数字标题中定义的数字算法不在算法标题中。

  • Accumulate: It accumulates the data, in given range, on given operation.

    累加:在给定范围内,在给定操作下累加数据。

  • Inner product: To calculate the inner product of two ranges of data.

    内部乘积:计算两个数据范围的内部乘积。

  • Partial sum: Partial sum calculates partial sum of given range of data and stores in other location.

    部分和:部分和计算给定数据范围的部分和并存储在其他位置。

  • Adjacent difference: It calculates the difference between adjacent items and stores in other location.

    相邻差异:计算相邻物料之间的差异并存储在其他位置。

使用C ++标准模板库(STL)的原因 (Reasons to Use C++ Standard Template Library (STL))

  • Code reuse. Instead of implementing lot of code we just reuse it.

    代码重用。 与其实施大量代码,我们只是重复使用它。
  • Efficiency (fast and use less resources). This is the reason that modern C++ compiler are usually tuned to optimize for C++ Standard Library.

    效率(快速且使用更少的资源)。 这就是通常对现代C ++编译器进行调整以针对C ++标准库进行优化的原因。
  • Accurate, less buggy.

    准确,越野车少。
  • Terse, readable code; reduced control flow.

    简洁易读的代码; 减少控制流。
  • Standardization, guaranteed availability.

    标准化,保证可用性。
  • A role model of writing library.

    编写库的榜样。
  • Good knowledge of data structures and algorithms.

    精通数据结构和算法。

Comment below if you have queries or found any information incorrect in above tutorial for Standard Template Library in C++.

如果您有疑问或在上面的C ++标准模板库教程中发现不正确的信息,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/07/c-standard-template-library-stl.html

stl标准模板库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值