STL算法、成员函数及编程问题解答
立即解锁
发布时间: 2025-08-19 01:36:40 阅读量: 1 订阅数: 6 


C++面向对象编程入门与实践
# STL算法、成员函数及编程问题解答
## 1. STL算法概述
STL(标准模板库)提供了丰富的算法,可用于各种数据处理任务。以下是一些常见STL算法的介绍:
| 算法名称 | 用途 | 参数 |
| --- | --- | --- |
| `set_difference` | 构造两个范围元素的有序差集,排序基于`comp`(仅适用于集合和多重集合) | `first1, last1, first2, last2, first3, comp` |
| `set_symmetric_difference` | 构造两个范围元素的有序对称差集 | `first1, last1, first2, last2, first3` |
| `push_heap` | 将`last - 1`位置的值放入`first`到`last`范围形成的堆中 | `first, last` 或 `first, last, comp` |
| `pop_heap` | 交换`first`和`last - 1`的值,并将`first`到`last - 1`范围变成堆 | `first, last` 或 `first, last, comp` |
| `make_heap` | 将`first`到`last`范围构造为堆 | `first, last` 或 `first, last, comp` |
| `sort_heap` | 对堆`first`到`last`中的元素进行排序 | `first, last` 或 `first, last, comp` |
| `min` | 返回两个对象中较小的一个 | `a, b` 或 `a, b, comp` |
| `max` | 返回两个对象中较大的一个 | `a, b` 或 `a, b, comp` |
| `max_element` | 返回范围中最大对象的迭代器 | `first, last` 或 `first, last, comp` |
| `min_element` | 返回范围中最小对象的迭代器 | `first, last` 或 `first, last, comp` |
| `lexicographical_compare` | 如果范围1中的序列按字母顺序排在范围2之前,则返回`true` | `first1, last1, first2, last2` 或 `first1, last1, first2, last2, comp` |
| `next_permutation` | 对范围中的序列进行一次排列 | `first, last` 或 `first, last, comp` |
| `prev_permutation` | 对范围中的序列进行一次逆排列 | `first, last` 或 `first, last, comp` |
### 1.1 广义数值操作
广义数值操作包括累加、内积、部分和等操作:
| 算法名称 | 用途 | 参数 |
| --- | --- | --- |
| `accumulate` | 对范围中的每个对象依次应用 `init = init + *iter` 或 `init = op(init, *iter)` | `first, last, init` 或 `first, last, init, op` |
| `inner_product` | 对范围1和范围2中对应的值依次应用 `init=init+(*iter1)*(*iter2)` 或 `init=op1(init,op2(*iter1,*iter2))` | `first1, last1, first2, init` 或 `first1, last1, first2, init, op1, op2` |
| `partial_sum` | 将范围1从开始到当前迭代器的值相加,并将和放入范围2的对应迭代器位置 | `first1, last1, first2` 或 `first1, last1, first2, op` |
| `adjacent_difference` | 计算范围1中相邻对象的差,并将结果放入范围2 | `first1, last1, first2` 或 `first1, last1, first2, op` |
### 1.2 迭代器要求
不同的算法对迭代器类型有不同的要求,如下表所示:
| 算法 | 随机访问迭代器 | 输入迭代器 | 输出迭代器 | 前向迭代器 | 双向迭代器 |
| --- | --- | --- | --- | --- | --- |
| `for_each` | | x | | | |
| `find` | | x | | | |
| `find_if` | | x | | | |
| `adjacent_find` | | x | | | |
| `count` | | x | | | |
| `count_if` | | x | | | |
| `mismatch` | | x | | | |
| `equal` | | x | | | |
| `search` | | x | | | |
| `copy` | | x | x | | |
| `copy_backward` | | | x | | x |
| `iter_swap` | | | | | x |
| `swap_ranges` | | | | | x |
| `transform` | | x | x | | |
| `replace` | | x | | | |
| `replace_if` | | x | | | |
| `replace_copy` | | x | x | | |
| `fill` | | | x | | |
| `fill_n` | | | x | | |
| `generate` | | | x | | |
| `generate_n` | | | x | | |
| `remove` | | x | | | |
| `remove_if` | | x | | | |
| `remove_copy` | | x | x | | |
| `remove_copy_if` | | x | x | | |
| `unique` | | | | | x |
| `unique_copy` | | x | x | | |
| `reverse` | | | | | x |
| `reverse_copy` | | x | x | | |
| `rotate` | | | | | x |
| `rotate_copy` | | x | x | | |
| `random_shuffle` | x | | | | |
| `partition` | | | | | |
| `stable_partition` | | | | | |
| `sort` | x | | | | |
| `stable_sort` | | | | | |
| `partial_sort` | x | | | | |
| `partial_sort_copy` | x | x | | | |
| `nth_element` | x | | | | |
| `lower_bound` | | | | | x |
| `upper_bound` | | | | | x |
| `equal_range` | | | | | x |
| `binary_search` | | | | | |
| `merge` | | x | x | | |
| `inplace_merge` | | | | | |
| `includes` | | x | | | |
| `set_union` | | x | x | | |
| `set_intersection` | | x | x | | |
| `set_difference` | | x | x | | |
| `set_symmetric_difference` | | x | x | | |
| `push_heap` | x | | | | |
| `pop_heap` | x | | | | |
| `make_heap` | x | | | | |
| `sort_heap` | x | | | | |
| `max_element` | | x | | | |
| `min_element` | | x | | | |
| `lexicographical_comparison` | | x | | | |
| `next_permutation` | | | | | |
| `prev_permutation` | | | | | |
| `accumulate` | | x | | | |
| `inner_product` | | x | | | |
| `partial_sum` | | x | x | | |
| `adjacent_difference` | | x | x | | |
## 2. 容器成员函数
不同容器具有不同的成员函数,以下是常见容器支持的成员函数列表:
| 成员函数 | 向量(Vector) | 列表(List) | 双端队列(Deque) | 集合(Set) | 多重集合(Multiset) | 映射(Map) | 多重映射(Multimap) | 栈(Stack) | 队列(Queue) | 优先队列(Priority Queue) |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| `operator==` | x | x | x | x | x | x | x | x | x | x |
| `operator!=` | x | x | x | x | x | x | x | x | x | x |
| `operator<` | x | x | x | x | x | x | x | x | x | x |
| `operator>` | x | x | x | x | x | x | x | x | x | x |
| `operator<=` | x | x | x | x | x | x | x | x | x | x |
| `operator>=` | x | x | x | x | x | x | x | x | x | x |
| `operator =` | x | x | x | | | | | | | |
| `operator[]` | x | | x | | | x | | | | |
| `operator*` | | x | | x | x | x | x | | | |
| `operator->` | | x | | x | x | x | x | | | |
| `operator ()` | | x | | | | | | x | x | x |
| `operator +` | x | | | | | | | | | |
| `operator -` | x | | | | | | | | | |
| `operator++` | | x | | x | x | x | x | | | |
| `operator--` | | x | | x | x | x | x | | | |
| `operator +=` | x | | x | | | | | | | |
| `operator -=` | x | | x | | | | | | | |
| `begin` | x | x | x | x | x | x | x | | | |
| `end` | x | x | x | x | x | x | x | | | |
| `rbegin` | x | x | x | x | x | x | x | | | |
| `rend` | x | x | x | x | x | x | x | | | |
| `empty` | x | x | x | x | x | x | x | x | x | x |
| `size` | x | x | x | x | x | x | x | x | x | x |
| `max_size` | x | x | x | x | x | x | x | | | |
| `front` | | x | x | | | | | x | x | |
| `back` | | x | x | | | | | | x | |
| `push_front` | | x | x | | | | | | | |
| `push_back` | x | x | x | | | | | | | |
| `pop_front` | | x | x | | | | | | | |
| `pop_back` | x | x | x | | | | | | | |
| `swap` | x | x | x | x | x | x | x | | | |
| `insert` | x | x | x | x | x | x | x | | | |
| `erase` | x | x | x | x | x | x | x | | | |
| `find` | | | | x | x | x | x | | | |
| `count` | | | | x | x | x | x | | | |
| `lower_bound` | | | | x | x | x | x | | | |
| `upper_bound` | | | | x | x | x | x | | | |
| `equal_range` | | | | x | x | x | x | | | |
| `top` | | | | | | | | x | | x |
| `push` | | | | | | | | x | x | x |
| `pop` | | | | | | | | x | x | x |
| `capacity` | x | | | | | | | | | |
| `reserve` | x | | | | | | | | | |
| `splice` | | x | | | | | | | | |
| `remove` | | x | | | | | | | | |
| `unique` | | x | | | | | | | | |
| `merge` | | x | | | | | | | | |
| `reverse` | | x | | | | | | | | |
| `sort` | | x | | | | | | | | |
## 3. 编程问题解答与练习
### 3.1 第1章问题解答
- 编程范式包括过程式和面向对象。
- 部分选择题答案:b、a、a、d等。
- 关键概念:数据隐藏、封装、多态等。
### 3.2 第2章问题解答与练习
#### 3.2.1 问题解答
- 输入输出相关:使用`cin`和`cout`进行输入输出,`IOSTREAM`和`IOMANIP`头文件的作用。
- 常量和变量:整数常量、字符常量、浮点常量等的识别。
- 赋值和算术运算符:如`=`、`+`、`-`等的使用。
#### 3.2.2 练习代码
```cpp
// ex2_1.cpp
// converts gallons to cubic feet
#include <iostream>
using namespace std;
int main()
{
float gallons, cufeet;
cout << "\nEnter quantity in gallons: ";
cin >> gallons;
cufeet = gallons / 7.481;
cout << "Equivalent in cublic feet is " << cufeet << endl;
return 0;
}
```
```cpp
// ex2_2.cpp
// generates table
#include <iostream>
#include <ioman
```
0
0
复制全文
相关推荐










