检查字符串是否是回文。C++

-1 c++ string palindrome

这有什么问题吗?

#include<bits/stdc++.h>
using namespace std;

bool isPalindrome(string str)
{
  char temp[1000];
  int len=str.length();
  int i=0;
  while(len)
  {
    len--;
    temp[i++]=str[len];
  }
  temp[i]='\0';
  if (strcmp(str,temp)==0)
     return true;
  else
     return false;
 }
Run Code Online (Sandbox Code Playgroud)

Ayx*_*xan 6

操作方法如下:

#include <string_view>
#include <algorithm>

bool isPalindrome(std::string_view const str) {
    return std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin());
}
Run Code Online (Sandbox Code Playgroud)

强调:

std::equal
std::string_view
std::string_view::rbegin