vaibhava96 vaibhava96 - 2 months ago 18
C++ Question

looping through a deque using a recursive function in c++

I am trying to create a recursive function in c++ that takes in a deque of integers as a parameter, loops through each element one by one, and returns the deque. I have found a few previous posts on StackOverflow that do something similar, but I am unable to understand what is happening in their answers. I am relatively new to C++. While it may be far easier and more efficient to do this by using an iterative algorithm, I am required to use recursion (it's an assignment question). Help is greatly appreciated.

Answer

it should be something like this

deque <int> x;
void Calc (deque <int> d){
    if (d.empty()) return;
    x.push_back(d.front());
    d.pop_front();
    Calc(d);
}
Comments