gpt4 book ai didi

C++ 模板返回带有类型名值的元组

转载 作者:行者123 更新时间:2023-11-28 01:40:33 26 4
gpt4 key购买 nike

<分区>

在我实现基于模板的链表的过程中,它使用键/值对,我想实现一个“popHead()”-方法。但是,我无法编译它。

/**
* Removes the first element in the list and returns it.
* @return first element, nullptr if list is empty
*/
std::tuple<K, V> popHead() {
auto tmp = head;
if (tmp) {
if (tmp->next) {
head = tmp->next;
} else {
head = nullptr;
}
return new std::tuple(tmp->key, tmp->value);
}

return nullptr;
};

这不起作用,因为需要指定类型。好的,所以它不知道元组包含的值应该具有哪种类型......但是......这也不起作用:

return new std::tuple<K, V>(tmp->key, tmp->value);

如何返回类型为 <K, V> 的元组? ?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com