在 C++ 中检查字符串是否为回文
    
    Jinku Hu
    2023年10月12日
    
    C++
    C++ String
    
- 
          
            使用 string复制构造函数与rbegin/rend方法来检查 C++ 中的字符串是否为回文
- 
          
            使用 std::equal方法在 C++ 中检查字符串回文
- 使用自定义函数在 C++ 中检查字符串是否是回文
 
本文将为大家讲解几种在 C++ 中如何检查一个字符串是否为回文的方法。
使用 string 复制构造函数与 rbegin/rend 方法来检查 C++ 中的字符串是否为回文
    
string 类对象支持使用运算符 == 进行比较,它可以用来寻找符合回文模式的字符串。由于回文意味着按相反的顺序匹配字符,我们需要用 rbegin 和 rend 迭代器构造一个新的字符串对象。其余的则根据需要在 if 语句中构造。
在下面的例子中,我们声明了两个字符串 - 单字回文和多字回文。请注意,尽管带空格的字符串符合定义模式,但它不能将带有空格的字符串作为回文式检测。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::equal;
using std::remove;
using std::string;
int main() {
  string s1 = "radar";
  string s2 = "Was it a cat I saw";
  if (s1 == string(s1.rbegin(), s1.rend())) {
    cout << "s1 is a palindrome" << endl;
  } else {
    cout << "s1 is not a palindrome" << endl;
  }
  if (s2 == string(s2.rbegin(), s2.rend())) {
    cout << "s2 is a palindrome" << endl;
  } else {
    cout << "s2 is not a palindrome" << endl;
  }
  return EXIT_SUCCESS;
}
输出:
s1 is a palindrome
s2 is not a palindrome
使用 std::equal 方法在 C++ 中检查字符串回文
尽管最后一个实现在单字字符串上完成了工作,但它带来了创建一个对象副本和比较它们的完整范围的开销。我们可以利用 std::equal 算法来比较同一个 string 对象范围的前半部分和后半部分。std::equal 如果给定的两个范围中的元素相等,则返回布尔值 true。请注意,函数只需要一个迭代器-s1.rbegin() 用于第二个范围,因为范围的结束是以 first2 + (last1 - first1) 计算的。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::equal;
using std::remove;
using std::string;
int main() {
  string s1 = "radar";
  string s2 = "Was it a cat I saw";
  equal(s1.begin(), s1.begin() + s1.size() / 2, s1.rbegin())
      ? cout << "s1 is a palindrome" << endl
      : cout << "s1 is not a palindrome" << endl;
  equal(s2.begin(), s2.begin() + s2.size() / 2, s2.rbegin())
      ? cout << "s2 is a palindrome" << endl
      : cout << "s2 is not a palindrome" << endl;
  return EXIT_SUCCESS;
}
输出:
s1 is a palindrome
s2 is not a palindrome
使用自定义函数在 C++ 中检查字符串是否是回文
以前的方法对多字的字符串有不足之处,我们可以通过实现一个自定义函数来解决。本例演示了布尔函数 checkPalindrome,它接受 string&参数,并将其值存储在本地 string 变量中。然后用 transform 算法处理本地对象,将其转换为小写字母,并因此用 eras-remove 删除其中的所有空白字符。最后,我们在 if 语句条件中调用 equal 算法,并返回相应的布尔值。不过要注意的是,如果字符串是由多字节字符组成的,这个方法会失败。因此,应该实现支持所有通用字符编码方案的小写转换方法。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::equal;
using std::remove;
using std::string;
bool checkPalindrome(string& s) {
  string tmp = s;
  transform(tmp.begin(), tmp.end(), tmp.begin(),
            [](unsigned char c) { return tolower(c); });
  tmp.erase(remove(tmp.begin(), tmp.end(), ' '), tmp.end());
  if (equal(tmp.begin(), tmp.begin() + tmp.size() / 2, tmp.rbegin())) {
    return true;
  } else {
    return false;
  }
}
int main() {
  string s1 = "radar";
  string s2 = "Was it a cat I saw";
  checkPalindrome(s1) ? cout << "s1 is a palindrome" << endl
                      : cout << "s1 is not a palindrome" << endl;
  checkPalindrome(s2) ? cout << "s2 is a palindrome" << endl
                      : cout << "s2 is not a palindrome" << endl;
  return EXIT_SUCCESS;
}
输出:
s1 is a palindrome
s2 is a palindrome
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Jinku Hu
    
