tags:

views:

45

answers:

2

I have a map like this:

map<prmNode,vector<prmEdge> > nodo2archi;

In a certain situation, I have to change an object's attribute in a value into the vector of prmEdge. This is the code:

prmNode par=freePathNode[z];
z++;
prmNode arr=freePathNode[z];

map<prmNode,vector<prmEdge> >::iterator it;
it=nodo2archi.find(par);
app.clear();
vector<prmEdge> arc;
arc=(*it).second;
app=arc;
vector<prmEdge>::iterator itarc;
for (itarc=app.begin(); itarc!=app.end(); itarc++){
    prmEdge arco=(*itarc);
    int a=arco.getFrom();
    int b=arco.getTo();

    int f=par.getIndex();
    int t=arr.getIndex();
    if ((a==f && b==t) || (b==f && a==t)){
        if (arco.getState()==0){
            if (!is_free_arco(par,arr)){
                togli_arco_par(arco,arr);
                    erased = true;
                    return erased;
            }
        else{   
            //ERROR
                    //it->second(it->second.begin()).setState(1);
            //(*it).second.begin().setState(1);
        }
          }
      }
  }    

I have to use the method setState to change an attribute into the map. The problem is that I don't have any method from map to make this operation, and in this way i have a compilation error. Could anybody give me an help to solve this?? Thank you very much!

+1  A: 

it->second.begin()->setState(1) should do it. it->second.begin() is a vector iterator, so you need -> to access the vector element.

If you need to access the other elements of the vector, you can of course use the vector's interface rather than iterators, e.g. it->second[2].setState(1).

Mike Seymour
Thank you very much. I think this is the correct way. I have another problem. For example, if the object to modify is the third of the vector, i wish to make like this: it->second.begin()+2->setState(1); but this not compile, how could I do?? thanks
livio8495
That would be it->second[2].setState(1);
Mike Seymour
Or `(it->second.begin()+2)->setState(1)`, if you particularly want to use an iterator.
Mike Seymour
thank you very much!
livio8495
Just be careful of running off the end of the vector with unchecked [] use, and additionally there is no check that the "nodo2archi.find(par)" call actually found the item.
Mark B
+1  A: 

This must work:

it->second.begin()->setState(1);

At least, the following my code works:

struct prmNode {};
struct prmEdge { void setState(int) {} };
map<prmNode,vector<prmEdge> > x;
map<prmNode,vector<prmEdge> >::iterator it = x.begin();
it->second.begin()->setState(1);
Vlad
Thank you very much. I think this is the correct way. I have another problem. For example, if the object to modify is the third of the vector, i wish to make like this:it->second.begin()+2->setState(1);but this not compile, how could I do??thanks
livio8495
As @Mike already suggested, (it->second.begin()+2)->setState(1) would do.
Vlad