call function with iterator parameter?

T

thomas

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<iterator>
#include<string>
#include<algorithm>

using namespace std;

void dfs(vector<int> &mp, vector<int>::iterator its, int i,
vector<int> &temp){
if(*its==0) return;
if(i==0) {
int s=1;
for(vector<int>::iterator it2(temp.begin()); it2<temp.end(); it2++)
s=s*(*it2);
if(s>1) mp.push_back(s); return;
}
for(vector<int>::iterator it_temp(its); *it_temp!=0; it_temp++){
temp.push_back(5); //<1>
dfs(mp, it_temp+1, i-1, temp);
temp.pop_back(); //<2>
}
}

int main(){
int m, k;
while(cin>>m>>k){
vector<int> mprime; mprime.push_back(3); mprime.push_back(4);
mprime.push_back(5);
mprime.push_back(6); mprime.push_back(0);
int msize = mprime.size();
for(int i=2; i<=msize; i++){
vector<int> temp; vector<int>::iterator itt(mprime.begin());
dfs(mprime, itt, i, temp);
mprime.push_back(0);
}
}
}

-----above code----

above is my code, when I call the recursive function dfs(), I got
serious troubles.
The compiler complains that I'm accessing some illegal address.
But if I comment the line marked <1> and <2>, everything is ok.
I just cannot figure out what went wrong.
brain exhausted...
 
S

sebastian

the problem is, once you have called push_back, previous iterators are
often invalidated. store an index instead?
 
T

thomas

the problem is, once you have called push_back, previous iterators are
often invalidated. store an index instead?

what do you mean by "invalidated"?
I'm calling push_back for "temp", doing nothing with its own iterators.
 
T

thomas

the problem is, once you have called push_back, previous iterators are
often invalidated. store an index instead?

Yeah! I got it. the mp vector iterator will be invalidated after
push_back. thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top