call function with iterator parameter?

Discussion in 'C++' started by thomas, May 1, 2008.

  1. thomas

    thomas Guest

    #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...
    thomas, May 1, 2008
    #1
    1. Advertising

  2. thomas

    sebastian Guest

    the problem is, once you have called push_back, previous iterators are
    often invalidated. store an index instead?
    sebastian, May 1, 2008
    #2
    1. Advertising

  3. thomas

    thomas Guest

    On May 2, 1:31 am, sebastian <> 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.
    thomas, May 2, 2008
    #3
  4. thomas

    thomas Guest

    On May 2, 1:31 am, sebastian <> 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!
    thomas, May 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,419
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    643
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,156
    Steven D'Aprano
    Apr 20, 2007
  5. AzamSharp
    Replies:
    2
    Views:
    176
Loading...

Share This Page