如何编写用于查找数组中一系列元素的函数?
我编写了一个名为 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()
时你永远不会剥离一个参数。递归永远不会终止,最终……堆栈溢出!
关于c++ - 如何编写用于查找数组中一系列元素的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50233490/