如何编写用于查找数组中一系列元素的函数?

How to write this function used in finding a series of elements in an array?

本文关键字:元素 函数 一系列 何编写 用于 查找 数组      更新时间:2023-10-16

如何编写用于查找数组中一系列元素的函数?

我编写了一个名为 find 的函数,用于查找元素,无论是否在数组中。现在我想使用 find(( 来查找一系列元素,如果所有元素都存在于此数组中,则该函数将返回 true 或返回 false:

template <typename T>
template <typename ...Args>
bool Vector<T>::find(const Args &...args) const {
std::deque<bool> findDeque;
findDeque.push_back(this->find(args...));        //The statement will lead to Exception: EXC_BAD_ACCESS (code=2, address=0x...)
auto begin {findDeque.cbegin()};
auto end {findDeque.cend()};
if(begin == end) {
return false;
}
while(begin != end) {
if(!*begin++) {
return false;
}
}
return true;
}

我试过了:

findDeque.push_back(this->find(args)...);

...
bool Vector<T>::find(Args &&...args) const {
...
findDeque.push_back(this->find(std::forward<Args>(args)...));
...

也许我研究参数包扩展得很糟糕.
我应该如何修改函数以使其成功找到。

你有无限递归,因为当你从自身调用find()时,你永远不会剥离一个参数。 递归永远不会接近终止,最终......堆栈溢出!