How to handle recursion in member functions?(如何处理成员函数中的递归?)
本文介绍了如何处理成员函数中的递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个empty
函数来清除链表:
void empty(Node* head) {
if (head->next) { empty(head->next); }
delete head;
head = nullptr;
}
但后来我为链表创建了一个类,所以现在不需要传递head
参数:
void empty() {
if (head->next) { empty(head->next); }
delete head;
head = nullptr;
}
但是empty(head->next)
行显然是错误的,因为empty
不接受任何参数。我的想法是在函数中创建一个函数(使用lambda),类似于:
void empty() {
std::function<void(Node*)> emptyWrapper = [&] (Node* l_head) {
if (l_head->next) { emptyWrapper(l_head->next); }
delete l_head;
l_head = nullptr;
};
emptyWrapper(head);
}
但我想知道有没有更好的办法。最近,Lambdas对我来说是一种理想的固定方式。
推荐答案
一般方法是声明公共成员函数,该成员函数又调用私有静态递归成员函数。
请注意,名称empty
听起来令人困惑。最好将函数命名为clear
。
给您
#include <functional>
//...
class List
{
public:
//...
void clear()
{
clear( head );
}
private:
static void clear( Node * &head )
{
if ( head )
{
delete std::exchange( head, head->next );
clear( head );
}
}
//...
}
无需定义辅助静态函数即可使用相同的方法。
void clear()
{
if ( head )
{
delete std::exchange( head, head->next );
clear();
}
}
这里是一个演示程序。
#include <iostream>
#include <iomanip>
#include <functional>
template <typename T>
class List
{
private:
struct Node
{
T data;
Node *next;
} *head = nullptr;
public:
void push_front( const T &data )
{
head = new Node { data, head };
}
friend std::ostream & operator <<( std::ostream &os, const List &list )
{
for ( Node *current = list.head; current; current = current->next )
{
os << current->data << " -> ";
}
return os << "null";
}
bool empty() const { return head== nullptr; }
void clear()
{
if ( head )
{
delete std::exchange( head, head->next );
clear();
}
}
};
int main()
{
List<int> list;
const int N = 10;
for ( int i = N; i != 0; )
{
list.push_front( i-- );
}
std::cout << list << '
';
list.clear();
std::cout << "list is empty " << std::boolalpha << list.empty() << '
';
return 0;
}
程序输出为
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
list is empty true
这篇关于如何处理成员函数中的递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何处理成员函数中的递归?


基础教程推荐
猜你喜欢
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30