在 C++ 中,对于字符串的反转,可以使用以下代码:

#include <iostream>
#include <algorithm>

int main() {
    std::string str = 'Hello, World!';
    
    std::reverse(str.begin(), str.end()); // 反转字符串
    
    std::cout << str << std::endl; // 输出反转后的字符串
    
    return 0;
}

这段代码使用了 <algorithm> 头文件中的 reverse 函数,该函数可以反转容器的元素。在这个例子中,我们使用 std::string 类型的 str 字符串,然后使用 reverse 函数将字符串反转。最后,我们通过 std::cout 输出反转后的字符串。

希望这个回答能够满足您的需求!

C++ 字符串反转:使用 reverse 函数

原文地址: https://www.cveoy.top/t/topic/JYf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录