简体   繁体   中英

Why does an iterator in a recursive function point to the beginning after a return?

I have a graph like data structure in which each node (of user defined gnode class) has an map with transitions from the node as keys and pointers to the respective nodes as values. I need to find all the leaf nodes in this data structure.

I've written a recursive function:

void leaves (gnode * node, int visited[],
             set<int, less <int> > * path, set<gnode*> * leaf_list) 
{
    int index = 0, elem; 
    bool check = false, transition_taken = false;

    for (this->map_itr = node->get_map()->begin();
         this->map_itr!= node->get_map()->end();
         this->map_itr++) 
    { 
        set <int, less<int> > transition = this->map_itr->first;
        elem = * transition.begin();
        index = std::distance(path->begin(), path->find(* transition.begin())); 
        
        if (transition.size() > 1) 
            check = std::includes (path->begin(), path->end(),
                        transition.begin(), transition.end())
                && !visited[index];
        else 
            check = path->find(elem) != path->end() 
                && !visited[index];
        if (check) {
            transition_taken = true;
            visited[index] = true;
            leaves (this->map_itr->second, visited, path, leaf_list);
    }
    if (!transition_taken) leaf_list->insert(node);
}

The issue I am having is this:

Suppose there is a transition from n1 → n2 → n3 (leaf). Once the leaf n3 has been encountered, there is a return to n2. Instead of continuing checking the transitions from where the call to n3 was made, map_itr in n2 now points to the start of the map transitions and goes through it again. I have a visited array that ensures that the call to n3 isn't made anymore but I want to know why it's iterating through the transitions in n2 from the beginning and how I can have it simply continue checking from the call point.

The reason why I think that this is the problem, is that I've printed the map_itr value for each iteration in each call and after n3, it starts with the first transition in n2, rather than continuing. If it's of any help, after the return to n2, if I try to access the element pointed to by map_itr , it gives me a segmentation error.

This isn't an answer, but rather a workaround that I'm using for the time being.

I created a map iterator as a member of the gnode class so that each node had a iterator associated with it. When I used this iterator instead of the iterator defined in the leaves function, everything ran smoothly.

I think for each recursive call, a new iterator isn't being created at the start of the call and instead the previous iterator's value is being overwritten. I assume this can be fixed with something like iterator map_itr = new iterator(); but I don't think you can do that in c++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM