判断回文哪里错了

whdugh 2013-09-05 10:08:04
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

/*
2013-9-5
编写一个方法用于验证指定的字符串是否为反转字符,返回true和false。请用递归算法实现。


两种情况,字符为奇数,最后剩下中间字符;
字符为偶数,最后没有字符

如:"aba","abba"

*/
bool is_PalindromicStr(string::iterator * p_start, string::iterator* p_end)
{
if (p_start == p_end)
return true;
else if (p_end == (p_start + 1))
return true;
else if (*p_start == *p_end)
is_PalindromicStr(p_start + 1, p_end - 1);
else
return false;
}
int main()
{
string str = "abba";

cout << is_PalindromicStr(str.begin(), str.end()-1) << endl;

return 0;
}

这一行报错 cout << is_PalindromicStr(str.begin(), str.end()-1) << endl;
哪里错了啊
...全文
168 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
citycal 2013-09-06
  • 打赏
  • 举报
回复
bool is_PalindromicStr(string::iterator p_start, string::iterator p_end)
{
	bool ret = false;
	//printf("%c %c\n",*p_start, *p_end);
	//printf( "%d %d\n",&(*(p_start + 1)), &(*(p_end - 1)) );
	if (*p_start == *p_end)
	{
		ret = true;
		if(p_start + 1 <= p_end - 1 )
		{
			if ( false == is_PalindromicStr(p_start + 1, p_end - 1) )
				ret = false;
			else
				ret = true;
		}
	}
	else
	{
		//printf("in else\n");
		ret = false;
	}
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//Object* p = Object::CreateObject("A");
	//delete p;

	string str = "abdggdba";
	cout << is_PalindromicStr(str.begin(), (str.end()-1)) << endl;

	system("pause");
	return 0;
}
vs2010编译通过。
艾薇儿More 2013-09-06
  • 打赏
  • 举报
回复
迭代器不建议此处用,指针好
大尾巴猫 2013-09-05
  • 打赏
  • 举报
回复
对象用作参数的时候,一般多用引用而不是指针 还有递归函数中逻辑有错误,我改了一下,注意比对不同之处
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

/*
2013-9-5
编写一个方法用于验证指定的字符串是否为反转字符,返回true和false。请用递归算法实现。


两种情况,字符为奇数,最后剩下中间字符;
字符为偶数,最后没有字符

如:"aba","abba"

*/
bool is_PalindromicStr(string::iterator& p_start, string::iterator& p_end) //改动
{
    if (p_start == p_end)
        return true;
    else if (p_end == (p_start - 1))  //改动
        return true;
    else if (*p_start == *p_end)
        return is_PalindromicStr(p_start + 1, p_end - 1); //改动
    else
        return false;
}
int main()
{
    string str = "abba";

    cout << is_PalindromicStr(str.begin(), str.end()-1) << endl;

    return 0;
}
nice_cxf 2013-09-05
  • 打赏
  • 举报
回复
刚测试了下,string的end返回不是无效,可以-1,奇怪的是vector的end返回虽然是无效,但是也可以-1....
chwhaiwei 2013-09-05
  • 打赏
  • 举报
回复
改为:cout << is_PalindromicStr(str.begin(), str.end()) << endl;
nice_cxf 2013-09-05
  • 打赏
  • 举报
回复
end返回大概是个无效指针,-1是不行的
whdugh 2013-09-05
  • 打赏
  • 举报
回复
引用 1 楼 woshinia 的回复:
迭代器没有重载“-”操作符吧,你又何必-1呢,正常的做法就是传begin()和end(),函数里也处理一下没有问题呀。 还有函数参数的类型是string::iterator *,而你传入的类型是string::iterator,多看看编译器的错误提示吧。
是啊 string::iterator就表示指针类型了 不过迭代器可以自减把
uchiha_iTachi 2013-09-05
  • 打赏
  • 举报
回复
引用 1 楼 woshinia 的回复:
迭代器没有重载“-”操作符吧,你又何必-1呢,正常的做法就是传begin()和end(),函数里也处理一下没有问题呀。 还有函数参数的类型是string::iterator *,而你传入的类型是string::iterator,多看看编译器的错误提示吧。
我也记得迭代器好像不能直接加减,但是刚试了一下,好像string的迭代器可以
woshinia 2013-09-05
  • 打赏
  • 举报
回复
迭代器没有重载“-”操作符吧,你又何必-1呢,正常的做法就是传begin()和end(),函数里也处理一下没有问题呀。 还有函数参数的类型是string::iterator *,而你传入的类型是string::iterator,多看看编译器的错误提示吧。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧