c++ - 如何在 C++ 中编写一个递归函数模板来检查作为参数提供的两个堆栈是否相同?
问题描述
我必须为我正在做的模块完成一个函数模板,问题如下:
编写一个递归函数模板,相同的,检查作为参数提供的两个堆栈是否相同。如果是这种情况,该函数应返回布尔值 true,否则应返回布尔值 false。使用以下标头:
template
bool sameS (stackType S1, stackType S2);
下面是我没有测试过的代码,但我问你它是否可以工作,如果不行,你将如何纠正它:
template <class Type>
bool identicalS (stackType<Type> S1, stackType<Type> S2);
template <class Type>
bool identicalS (stackType<Type> S1, stackType<Type> S2)
{
Type lh, rh;
int ret = 0;
if ( (lh = s1.pop()) == (rh = s2.pop()) ) {
if ( !s1.isEmptyStack() and !s2.isEmptyStack() ) return identicalS(s1, s2);
else ret = 1;
}
s1.push(lh);
s2.push(rh);
return ret;
}
解决方案
推荐阅读
- jsreport - 无法将自定义配方与 .Net Core 一起使用
- arrays - XML到数组使用PHP但返回错误
- google-sheets - Google Sheets QUERY 函数 - 是否可以在插入列时写入不中断?
- c# - Rider 要求安装 .NET Framework 4.7.2 Developer Pack,即使它已安装
- mongodb - 使用 mongodb+srv 连接到作为 statefulset 部署的 mongodb
- c - 如何使用 ptrace 找到 CALL 和 RET 号码?
- php - 在 PHP 数组中使用值
- c# - Firebase 数据 - 错误转换值
- jquery - 使用 Rails 5 从多个表数据中按时间顺序馈送
- javascript - Jquery stop image blink before fade out