it编程 > 编程语言 > C/C++

C++ 标准库中的reverse 函数使用示例

27人参与 2026-01-12 C/C++

一.函数原型

template <class bidirectionaliterator>
void reverse(bidirectionaliterator first, bidirectionaliterator last);

二.函数参数

first:指向要反转序列起始位置的迭代器
last:指向要反转序列结束位置的下一个位置的迭代器(左闭右开区间 [first, last))

三.使用示例

1.反转数组

#include <iostream>
#include <algorithm>
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    std::reverse(arr, arr + n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";  // 输出: 5 4 3 2 1
    }
    return 0;
}

2.反转vector

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    reverse(vec.begin(), vec.end());
    for (int num : vec) {
        cout << num << " ";  // 输出: 5 4 3 2 1
    }
    return 0;
}

3.反转string

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
    string str = "hello, world!";
    reverse(str.begin(), str.end());
    cout << str << endl;  // 输出: !dlrow ,olleh
    return 0;
}

4,反转部分元素

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8};
    // 只反转中间部分元素 [2, 3, 4, 5, 6] -> [6, 5, 4, 3, 2]
    reverse(vec.begin() + 1, vec.end() - 1);
    for (int num : vec) {
        cout << num << " ";  // 输出: 1 7 6 5 4 3 2 8
    }
    return 0;
}

四.复杂度分析

时间复杂度:o(n),其中 n 是 last - first,执行大约 n/2 次交换
空间复杂度:o(1),原地操作,不需要额外空间

五.注意事项

  1. reverse 函数要求迭代器是双向迭代器(bidirectionaliterator)。
  2. 可以用于所有支持双向迭代器的容器:vectordequeliststring数组
  3. reverse 会修改原容器,如果不希望修改原容器,可以使用 reverse_copy

六.相关函数

1.reverse_copy

reverse函数不保证稳定性(因为交换元素可能会改变相等元素的相对顺序,但通常我们使用reverse时并不关心这个,因为元素值不同,且即使相同,反转后顺序也变了)。
c++标准库还提供了reverse_copy函数,它可以将反转的结果复制到另一个序列中,而不改变原序列。

template <class bidirectionaliterator, class outputiterator>
outputiterator reverse_copy(bidirectionaliterator first,
bidirectionaliterator last,
outputiterator result);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    vector<int> src = {1, 2, 3, 4, 5};
    vector<int> dst(src.size());
    reverse_copy(src.begin(), src.end(), dst.begin());
    cout << "原序列: ";
    for (int num : src) {
        cout << num << " ";  // 输出: 1 2 3 4 5
    }
    cout << "\n反转后的副本: ";
    for (int num : dst) {
        cout << num << " ";  // 输出: 5 4 3 2 1
    }
    return 0;
}

2.自定义反转算法实现

#include <iostream>
#include <vector>
using namespace std;
// 手动实现 reverse 功能
template<typename t>
void my_reverse(t begin, t end) {
    while (begin != end && begin != --end) {
        swap(*begin, *end);
        ++begin;
    }
}
int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    my_reverse(vec.begin(), vec.end());
    for (int num : vec) {
        cout << num << " ";  // 输出: 5 4 3 2 1
    }
    return 0;
}

3.与反向迭代器的区别

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    // 使用 reverse 函数修改原容器
    reverse(vec.begin(), vec.end());
    cout << "使用 reverse 后: ";
    for (int num : vec) {
        cout << num << " ";  // 输出: 5 4 3 2 1
    }
    cout << endl;
    // 重置 vector
    vec = {1, 2, 3, 4, 5};
    // 使用反向迭代器(不修改原容器,只是反向遍历)
    cout << "使用反向迭代器遍历: ";
    for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
        cout << *it << " ";  // 输出: 5 4 3 2 1
    }
    cout << endl;
    cout << "原容器未被修改: ";
    for (int num : vec) {
        cout << num << " ";  // 输出: 1 2 3 4 5
    }
    return 0;
}

到此这篇关于c++ 标准库中的reverse 函数的文章就介绍到这了,更多相关c++ 标准库reverse 函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

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

01-12

C/C++中 __asm volatile 函数的实现

01-12

C++ 标准库入门到精通

01-14

C++中list实现双向循环链表详细解析

01-15

一文详解为什么越来越多项目开始从JDK 8升级到JDK 21

01-11

C++ io_uring的使用小结

01-11

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论