一、什么是reverse()函数?
reverse()是C++ STL中一个非常有用的算法函数,用于反转序列容器(如vector、list、deque、string等)中元素的顺序。它属于<algorithm>头文件中的算法库,可以高效地将容器中的元素顺序完全颠倒。
二、基本用法
1.反转整个容器
#include <iostream> #include <algorithm> // reverse()函数在这里 #include <vector> using namespace std; int main() { vector<int> numbers = {1, 2, 3, 4, 5}; for (int num : numbers) { cout << num << " "; } cout << std::endl; // 输出: 1 2 3 4 5 // 使用reverse()反转整个vector reverse(numbers.begin(), numbers.end()); for (int num : numbers) { cout << num << " "; } cout << std::endl; // 输出: 5 4 3 2 1 return 0; }2.反转字符串
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string text = "Hello, World!"; // 反转字符串 reverse(text.begin(), text.end()); cout << "反转后: " << text << std::endl; // 输出: !dlroW ,olleH // 判断回文字符串 string palindrome = "racecar"; string original = palindrome; reverse(palindrome.begin(), palindrome.end()); if (original == palindrome) { cout << original << " 是回文字符串!" << endl; } else { cout << original << " 不是回文字符串!" << endl; } return 0; }3.反转部分元素
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> numbers = {10, 20, 30, 40, 50, 60, 70}; for (int num : numbers) { cout << num << " "; } cout << endl; // 输出: 10 20 30 40 50 60 70 // 只反转中间部分(第2到第5个元素,索引1到4) reverse(numbers.begin() + 1, numbers.begin() + 5); for (int num : numbers) { cout << num << " "; } cout << endl; // 输出: 10 50 40 30 20 60 70 return 0; }三、reverse()函数的特点
1.参数:两个迭代器
// 函数签名 template<class BidirIt> void reverse(BidirIt first, BidirIt last); // first: 指向要反转的第一个元素 // last: 指向要反转的最后一个元素的下一个位置2.时间复杂度:O(n)
需要遍历一半的元素进行交换
对于n个元素,大约需要n/2次交换
3.原地操作
reverse()直接在原容器上修改,不创建新容器不需要额外的内存空间(除了临时变量)
4.支持双向迭代器
需要容器的迭代器支持双向移动(++和--)
适用于vector、deque、list、string等
四、实际应用场景
1.旋转数组
#include <iostream> #include <algorithm> #include <vector> using namespace std; void rotateArray(vector<int>& arr, int k) { int n = arr.size(); k = k % n; // 处理k大于数组长度的情况 if (k == 0) return; // 方法:三次反转法 // 1. 反转整个数组 reverse(arr.begin(), arr.end()); // 2. 反转前k个元素 reverse(arr.begin(), arr.begin() + k); // 3. 反转剩余元素 reverse(arr.begin() + k, arr.end()); } int main() { vector<int> numbers = {1, 2, 3, 4, 5, 6, 7}; cout << "原始数组: "; for (int num : numbers) { cout << num << " "; } cout << endl; // 向右旋转3位 rotateArray(numbers, 3); cout << "旋转3位后: "; for (int num : numbers) { cout << num << " "; } cout << std::endl; // 输出: 5 6 7 1 2 3 4 return 0; }2.反转单词顺序
#include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> using namespace std; string reverseWords(string s) { // 先反转整个字符串 reverse(s.begin(), s.end()); stringstream ss(s); string word, result; vector<std::string> words; // 分割单词 while (ss >> word) { // 反转每个单词(恢复原始顺序) reverse(word.begin(), word.end()); words.push_back(word); } // 重新组合 for (size_t i = 0; i < words.size(); ++i) { if (i > 0) result += " "; result += words[i]; } return result; } int main() { string sentence = "the sky is blue"; cout << "原始句子: " << sentence << endl; cout << "单词反转后: " << reverseWords(sentence) << endl; // 输出: blue is sky the return 0; }3.数字反转
#include <iostream> #include <algorithm> #include <string> using namespace std; int reverseNumber(int num) { // 处理负数 bool isNegative = num < 0; if (isNegative) num = -num; // 转换为字符串 string numStr = to_string(num); // 反转字符串 reverse(numStr.begin(), numStr.end()); // 转换回整数 int result = stoi(numStr); // 恢复负号 return isNegative ? -result : result; } int main() { int num1 = 12345; int num2 = -6789; cout << num1 << " 反转后: " << reverseNumber(num1) << endl; // 54321 cout << num2 << " 反转后: " << reverseNumber(num2) << endl; // -9876 return 0; }五、与其他反转方法的比较
1.手动反转
#include <iostream> #include <vector> using namespace std; void manualReverse(vector<int>& vec) { int left = 0; int right = vec.size() - 1; while (left < right) { // 交换元素 swap(vec[left], vec[right]); left++; right--; } } int main() { vector<int> numbers = {1, 2, 3, 4, 5}; cout << "手动反转前: "; for (int num : numbers) { cout << num << " "; } cout << endl; manualReverse(numbers); cout << "手动反转后: "; for (int num : numbers) { cout << num << " "; } cout << endl; return 0; }2.使用rbegin()和rend()
#include <iostream> #include <vector> using namespace std; int main() { vector<int> numbers = {1, 2, 3, 4, 5}; cout << "反向遍历(不修改原数组): "; for (auto it = numbers.rbegin(); it != numbers.rend(); ++it) { cout << *it << " "; } cout << endl; // 输出: 5 4 3 2 1 cout << "原数组未改变: "; for (int num : numbers) { cout << num << " "; } cout << endl; // 输出: 1 2 3 4 5 return 0; }六、C++20及后续版本
在C++20中,引入了范围库,可以使用更简洁的语法:
#include <iostream> #include <vector> #include <algorithm> #include <ranges> // C++20 using namespace std; int main() { vector<int> numbers = {1, 2, 3, 4, 5}; // C++20范围视图(不修改原容器) auto reversedView = numbers | std::views::reverse; cout << "使用C++20范围视图反向遍历: "; for (int num : reversedView) { cout << num << " "; } cout << endl; // 输出: 5 4 3 2 1 cout << "原数组未改变: "; for (int num : numbers) { cout << num << " "; } cout << endl; // 输出: 1 2 3 4 5 return 0; }七、总结&今日题目
reverse()是STL中一个简单但强大的算法函数,用于反转容器中元素的顺序。记住以下要点:
包含头文件:
#include <algorithm>参数:两个迭代器,表示要反转的范围
时间复杂度:O(n),进行大约n/2次交换
原地操作:直接修改原容器
适用容器:支持双向迭代器的容器(vector、deque、list、string、array等)
常见用途:
反转字符串或数组
判断回文
旋转数组
反转单词顺序
注意事项:
确保迭代器范围有效
list有自己的
reverse()成员函数,效率更高使用
reverse_copy()如果需要保留原容器
今日题目:回文检查器
问题描述
你正在开发一个文本处理工具,需要检查用户输入的单词或句子是否是回文(palindrome)。回文是指正读和反读都一样的字符串(忽略大小写、空格和标点符号)。
功能要求:
提示用户输入一个单词或句子
清理输入(移除空格、标点,转换为小写)
使用
reverse()函数辅助判断是否为回文显示判断结果
输入格式
一个字符串(可能包含空格和标点符号)。
输出格式
判断结果和详细的分析过程。
解答:
#include <iostream> #include <algorithm> #include <string> #include <cctype> uisng namesapce std; int main() { string input; getline(cin, input); string cleaned; for (char ch : input) { if (isalnum(ch)) { cleaned += tolower(ch); } } // 使用reverse()判断 string reversed = cleaned; reverse(reversed.begin(), reversed.end()); // 输出结果 cout << "结果: "; if (cleaned == reversed) { cout << "是回文" << endl; } else { cout << "不是回文" << endl; } return 0; }