Stack Overflow Asked by Steven Zhou on December 30, 2021
I try to use recursive function to output all parameters of an int type vector, it works butenter image description here there is a Debug Assertion failed occurred. please help me point out where is problem from. thanks in advance.
#include<iostream>
#include<vector>
using namespace std;
void val(vector<int>::const_iterator beg, vector<int>::const_iterator end)
{
if (beg!=end)
cout << *beg << endl;
val((beg + 1),end);
}
int main()
{
int n;
vector<int> vec;
while (cin >> n)
vec.push_back(n);
val(vec.begin(),vec.end());
}
You haven't provided where the recursion should stop. You can use something like what follows
void val(vector<int>::const_iterator beg, vector<int>::const_iterator end)
{
if(beg == end) return;
cout << *beg << endl;
val(beg + 1,end);
}
Answered by asmmo on December 30, 2021
Nearly right but don't call yourself recursively once you have reached the end of the vector
void val(vector<int>::const_iterator beg, vector<int>::const_iterator end)
{
if (beg!=end)
{
cout << *beg << endl;
val((beg + 1),end);
}
}
Answered by john on December 30, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP