迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中反转字符串

作者:迹忆客 最近更新:2023/04/09 浏览次数:

本文将介绍如何在 C++ 中反转字符串。


使用字符串构造函数来反转字符串

std::basic_string 有构造函数,它可以用范围内的内容构建一个字符串。然后,我们可以声明一个新的字符串变量,并向其构造函数提供原始字符串变量的反向迭代器-tmp_s。下面的例子演示了这个方法,并输出两个字符串进行验证。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed (tmp_s.rbegin(), tmp_s.rend());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

输出:

This string will be reversed
desrever eb lliw gnirts sihT

使用 std::reverse() 算法反转字符串

std::reverse 方法来自 <algorithm>STL 库,它将范围内元素的顺序反转。该方法对作为参数传递的对象进行操作,并不返回数据的新副本,所以我们需要声明另一个变量来保存原始字符串。

需要注意的是,如果算法未能分配内存,reverse 函数会抛出 std::bad_alloc 异常。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed(tmp_s);
    reverse(tmp_s_reversed.begin(), tmp_s_reversed.end());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

使用 std::copy() 算法反转字符串

std::copy 是另一种强大的算法,可以用于多种情况。该方案初始化一个新的字符串变量,并使用内置的 resize 方法修改其大小。接下来,我们调用 copy 方法,用原始字符串的数据填充声明的字符串。但要注意,前两个参数应该是源范围的反向迭代器。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed;
    tmp_s_reversed.resize(tmp_s.size());
    copy(tmp_s.rbegin(), tmp_s.rend(), tmp_s_reversed.begin());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

在不需要存储反向字符串数据的情况下,我们可以使用 copy() 算法直接将字符串数据按反向顺序输出到控制台,如以下代码示例所示。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    copy(tmp_s.rbegin(), tmp_s.rend(),
         std::ostream_iterator<char>(cout,""));

    return EXIT_SUCCESS;
}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便