rbegin和rend()在这个函数中做了什么? [英] What does rbegin and rend() do in this function?

查看:781
本文介绍了rbegin和rend()在这个函数中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rbegin()和rend()在下面的函数中做了什么以确定输入是否是回文?

  bool palindromeCheck(string input){
if(input == string(input.rbegin(),input.rend())){
return true;
}
else {
return false;
}
}


解决方案

One std :: string 的构造函数c> 需要一对迭代器作为输入。 rbegin() 将一个反向迭代器返回到原始 input 字符串的最后一个字符,并且 rend() 返回一个反向迭代器到原始输入字符串(即一个超过字符串的结尾)。

结果是一个新字符串是反转版本原始构造,如果这个新的字符串等于原来的那么它是一个回文,并且该函数返回true。


What exactly does rbegin() and rend() do in the following function to determine whether input is a palindrome or not?

bool palindromeCheck(string input) {
    if (input == string(input.rbegin(), input.rend())) {
        return true;
    } 
    else {
        return false;
    }
}

解决方案

One of the constructors for std::string takes a pair of iterators as inputs. rbegin() returns a reverse iterator to the last character of the original input string, and rend() returns a reverse iterator to the character preceding the first character of the original input string (i.e. one past the "end" of the string).

The result is that a new string that is the reversed version of the original is constructed, and if that new string is equal to the original then it's a palindrome and the function returns true.

这篇关于rbegin和rend()在这个函数中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆