c++ - 如何传递回调函数将项目推送到向量中
问题描述
所以在我的数据结构类中,我正在编写一个中序遍历函数来遍历 BST 中的节点并将项目推送到向量中。然而,这是我第一次处理回调函数和 lambda。以下是我尝试过的,但出现类型转换错误。任何见解将不胜感激。
template<class ItemType> // Recursion launcher
void BinarySearchTree<ItemType>::inorderTraverse(void visit(ItemType& item)) const {
if(rootPtr == nullptr) return;
inorder(rootPtr, visit);
}
template<class ItemType> // I am not allowed to change this function signature
void BinarySearchTree<ItemType>::inorder(BinaryNode<ItemType>* node, void visit(ItemType& item)) const
{
if(node->left) inorder(node->left, visit);
ItemType item = node->getItem();
visit(item);
if(node->right) inorder(node->right, visit);
}
template<class ItemType>
void BinarySearchTree<ItemType>::function() {
vector<ItemType> temp;
// The problem is the function below
auto function = [&temp](auto item){temp.emplace_back(item);}
inorderTraverse(function);
}
解决方案
推荐阅读
- php - 在 IIS 服务器上从 PHP 运行 Python 脚本
- wordpress - 如何使用自定义元值从 woocommerce 获取产品
- swift - 在 OSX 上录制音频(AVAudioSession 不可用)
- c# - 登录应用程序主屏幕后,需要在 15 分钟内提醒用户即将到来的约会(如果已安排)
- python - 按计数从熊猫数据框中提取最常见的 2 列组合
- angular - MatTableDataSource 无法读取 Angular 7 中未定义的属性“数据”
- angularjs - 仅显示日历特定日期的标题
- nginx - 在 nginx 中使用 lua 脚本发布请求处理
- azure - Azure LetsEncrypt 无法访问我的函数的 .well-known/acme-challenge
- karate - 当字符串值中包含“-”时,该值在 JSON 对象中被截断