zl程序教程

您现在的位置是:首页 >  Java

当前栏目

C++进阶教程:C++ 标准模板库初学者指南

2023-02-19 12:18:02 时间

C++ 是初学者可能遇到的最强大、最令人生畏的编程语言之一。原因很简单。它需要大量代码来实现所需的输出。标准模板库或 STL 可以帮助您解决这个难题。

考虑到为排序和搜索等功能编写代码所消耗的时间和精力,STL 可以帮助您只用一行代码执行所有这些操作。这个库对于解决问题和准备技术面试非常有用。

什么是标准模板库?

标准模板库或 STL 是一个 C++ 库,由预构建的函数和容器组成。它包括一些用于常见数据结构(如向量、堆栈、队列)的突出模板类,以及一些方便的算法函数(如二进制搜索),以使编程更容易。

C++ 中的标准模板库由四个组件组成:

  1.  算法
  2.  容器
  3.   功能
  4.  迭代器

让我们更深入地了解一下算法和容器,因为它们是 STL 中最常用的组件。

STL 中的算法

<algorithm>头文件是 STL的一部分,它由几个算法函数组成,可以使用这些算法函数来代替手动编码它们。包括的一些算法是二进制搜索、排序和反向,它们非常有用。

首先,您需要在 C++ 文件中导入<algorithm>标头。语法如下:

#include <algorithm>

对于即将出现的方法,以具有 {6, 2, 9, 1, 4} 值的数组变量为例。

int arr[] = {6, 2, 9, 1, 4};

sort()

sort()函数可帮助您按升序对指定数据结构内的所有元素进行排序。这个函数有两个参数:开始迭代器和结束迭代器。

语法:

sort(start_iterator, end_iterator);

这是一个简单的例子:

sort(arr, arr+5);
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

输出:

1 2 4 6 9

reverse()

reverse()函数反转指定数据结构中元素的 顺序。它接受两个参数:开始迭代器和结束迭代器。

语法:

reverse(start_iterator, end_iterator);

这是上述方法的一个简短示例:

reverse(arr, arr+5);
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

输出:

4 1 9 2 6

*min_element() 和 *max_element()

函数 *max_element() 和*min_element() 分别返回指定数据结构内的最大值和最小值。这两个函数都接受两个参数:开始迭代器和结束迭代器。

语法:

*max_element(start_iterator, end_iterator);
*min_element(start_iterator, end_iterator);

让我们找出这些函数在示例数组上调用它们时返回的值:

cout << *max_element(arr, arr+5) << endl;
cout << *min_element(arr, arr+5) << endl;

输出:

9
1

binary_search()

binary_search ()方法用于查找指定值是否存在于数据结构中。它接受三个参数:开始迭代器、结束迭代器和要查找的值。

二进制搜索仅适用于已排序的数据结构。因此,您需要先调用sort()方法,然后再调用binary_search()方法。

语法:

binary_search(start_iterator, end_iterator, value_to_find)

这是此方法的演示:

sort(arr, arr+5);
binary_search(arr, arr+5, 2) ? cout << "Element found" : cout << "Element not found";
binary_search(arr, arr+5, 7) ? cout << "Element found" : cout << "Element not found";

输出:

Element found
Element not found

count()

count()方法返回数据结构中指定值的出现次数。它接受三个参数:开始迭代器、结束迭代器和要计数的值。

语法:

count(start_iterator, end_iterator, value_to_count);

这是此方法的示例:

cout << count(arr, arr+5, 2) << endl;

输出:

1

STL 中的容器

容器是存储对象和数据的数据结构。向量、列表、堆栈、队列、集合和映射是根据指定的原始数据类型在其中存储数据的一些示例。您可以通过在 C++ 文件中导入它们各自的标头来使用这些容器。

在初始化容器变量时,您需要在 <>括号内提及原始数据,例如 int、  char、  string 。

让我们更详细地探索其中一些容器:

向量 Vector

向量是可调整大小且使用灵活的动态数组。当您从向量中插入或删除元素时,它会自动调整向量的大小。这类似于 Java 中的ArrayList 数据结构。

句法:

#include <vector>

vector<data_type> vaiable_name;

以下是一些重要的向量方法:

  1.  push_back(value):此方法将数据附加到向量。
  2.  pop_back():此方法从向量中删除最后一个元素。
  3.  insert(index, value):此方法在指定位置的元素之前插入新元素。
  4.  size():此方法返回向量的大小。
  5.  empty():此方法检查向量是否为空。
  6.  front():此方法返回向量的第一个值。
  7.  back() : back 方法返回向量的最后一个值。
  8.  at(index):该方法返回指定位置的值。
  9.  erase(index):擦除方法从给定索引中删除元素。
  10.  clear():此方法清除向量中的所有项目。
vector < int > v = { 23, 12, 56, 10 };
v.push_back(5);
v.push_back(25);
v.pop_back();
auto i = v.insert(v.begin() + 1, 7);
cout << "The size of the given vector " << v.size() << endl;
if (v.empty()) {
cout << "Vector is empty" << endl;
} else {
cout << "Vector is not empty" << endl;
}
cout << "Element at the first position is " << v.front() << endl;
cout << "Element at the last position is " << v.back() << endl;
cout << "Element at the given position is " << v.at(4) << endl;
v.erase(v.begin() + 1);
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}

输出:

The size of the given vector 6
Vector is not empty
Element at the first position is 23
Element at the last position is 5
Element at the given position is 10
23 12 56 10 5

队列 Queue

在队列数据结构中,元素从后面插入,从前面删除。因此,它遵循 FIFO(“先进先出”)方法。

句法:

#include <queue>
queue<data_type> variable_name;

以下是一些重要的队列方法:

  1.  push(value ):此方法将元素添加到队列中。
  2.  pop():此方法删除队列的第一个元素。
  3.  size():此方法返回队列的大小。
  4.  front():此方法返回队列的第一个元素。
  5.  back():此方法返回队列的最后一个元素。
queue < int > q;
q.push(30);
q.push(40);
q.push(50);
q.push(60);
q.push(70);
cout << "The first element is " << q.front() << endl;
cout << "The last element is " << q.back() << endl;
cout << "The size of queue is " << q.size() << endl;
q.pop();
cout << "Printing all the elements of the Queue" << endl;
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}

输出:

The first element is 30
The last element is 70
The size of the queue is 5
Printing all the elements of the Queue
40 50 60 70

堆 Stack

堆栈容器在 LIFO 方法上运行。LIFO 代表“后进先出”。数据从同一端推送和弹出。

语法:

#include <stack>
stack<data_type> variable_name;

以下是一些重要的堆栈方法:

  1.  push(value ):此方法将元素压入堆栈。
  2.  pop():此方法删除堆栈的顶部元素。
  3.  top():此方法返回栈中最后一个元素的值。
  4.  size():此方法返回堆栈的大小。
  5.  empty():此方法检查堆栈是否为空。
stack < int > s;
s.push(30);
s.push(40);
s.push(50);
s.push(60);
cout << "The top of the stack contains " << s.top() << endl;
s.pop();
cout << "The top of the stack after performing pop operation: " << s.top() << endl;
cout << "Printing all elements of the stack" << endl;
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}

输出:

The top of the stack contains 60
The top of the stack after performing pop operation: 50
Printing all elements of the stack
50 40 30

集合 Set

集合容器用于保存唯一值,元素的值一旦插入集合就不能更改。集合中的所有元素都以排序方式存储。set 容器类似于Python 中的 set 数据结构

句法:

#include <set>

set<data_type> variable_name;

以下是一些重要的设置方法:

  1.  insert(value):此方法在集合中插入元素。
  2.  begin():此方法将迭代器返回到集合的第一个元素。
  3.  end():此方法将迭代器返回到集合的最后一个元素。
  4.  size():此方法返回集合的大小。
  5.  empty():此方法检查集合是否为空。
  6.  find(value):此方法将迭代器返回到参数中传递的元素。如果未找到该元素,则此函数将迭代器返回到集合的末尾。
  7.  erase(value):此方法从集合中删除指定的元素。
set < int > s;
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
s.insert(60);
s.insert(60);
s.insert(60);
auto i = s.begin();
cout << "Element at the first position " << * i << endl;
cout << "The size of the set " << s.size() << endl;
s.find(20) != s.end() ? cout << "Element found" << endl : cout << "Element not found" << endl;
s.erase(30);
cout << "Printing all the elements" << endl;
for (auto i = s.begin(); i != s.end(); i++) {
cout << * i << " ";
}

输出:

Element at the first position 20
The size of the set 5
Element found
Printing all the elements
20 40 50 60

C++ 不一定很难

就像所有其他技能一样,练习对于充分利用 STL 至关重要。这些容器和算法可以帮助您节省大量时间并且易于使用。从练习上面显示的示例开始,您最终也会开始在自己的项目中使用它。

但是,如果这是您第一次学习 C++,请先学习基础知识,然后再继续了解 STL。