Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > call function with iterator parameter?

Reply
Thread Tools

call function with iterator parameter?

 
 
thomas
Guest
Posts: n/a
 
      05-01-2008
#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...
 
Reply With Quote
 
 
 
 
sebastian
Guest
Posts: n/a
 
      05-01-2008
the problem is, once you have called push_back, previous iterators are
often invalidated. store an index instead?
 
Reply With Quote
 
 
 
 
thomas
Guest
Posts: n/a
 
      05-02-2008
On May 2, 1:31*am, sebastian <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
thomas
Guest
Posts: n/a
 
      05-02-2008
On May 2, 1:31*am, sebastian <(E-Mail Removed)> wrote:
> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments