Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
If you need the function to only accept one argument of type Vector, then the general solution is to truncate the vector on each recursion. {1,2,3,4,5,6} print last value remove last value from vector pass new vector {1,2,3,4,5} to recursive call Run till vector is empty. You need to understand how to create a new vector from an old vector using iterators, and where the iterators point to. https://code.sololearn.com/cbSxLu61HI7k/?ref=app
17th Sep 2020, 6:54 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Here's my alternative implementation of what Hatsy Rei has just explained void print(vector<int>& v){ if (v.size() == 0) return; cout << v.back() << " ";//print the last element v.pop_back();//remove that last element print(v); //repeat } int main(){ vector<int> v={1,2,3,4,5,6}; print(v);// 6 5 4 3 2 1 }
17th Sep 2020, 11:26 AM
Anthony Maina
Anthony Maina - avatar