#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// 函数用于反转字符串
void reverseString(string &str) {
// 使用标准库函数reverse来反转字符串
reverse(str.begin(), str.end());
}
int main() {
string s = "hello";
cout << "原始字符串: " << s << endl;
reverseString(s);
cout << "反转后的字符串: " << s << endl;
return 0;
}
#include <iostream>: 引入输入输出流库,用于 cout 和 cin。#include <string>: 引入字符串库,用于使用 std::string 类型。#include <algorithm>: 引入算法库,用于调用 std::reverse 函数。using namespace std;: 使用标准命名空间,避免每次使用标准库函数时都要加上 std:: 前缀。void reverseString(string &str): 定义一个函数 reverseString,它接受一个引用参数 str,并使用 std::reverse 来反转字符串。main 函数:s 并初始化为 "hello"。reverseString 函数来反转字符串。这个程序展示了如何使用 C++ 标准库中的 std::reverse 函数来轻松实现字符串的反转。
上一篇:c++ transform
下一篇:c++多态实现的三种形式
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站