# help with Map and Vector

• 12-06-2002
geeoff99
max of 2 in vector in map<int, vector<EDGE*> > ??
Sorry people, maybe I was not specific enough originally. This should be a simple problem for you gurus!

I have a map:

map<int, vector<EDGE*> > myMap

when I myMap.find(5) and then
PAIR.second.push_back(EDGE*) ... I am only able to add two elements, the third addition overwrites the last element. Aren't Vector dynamic? What is going on. Code is below

==========================>

I am looking up a Vector stored in a Map via an int. This seems to work ok. I am able to add 2 elements to the Vector using push_back and that is it. The Vector stored at the Map entry of int X overwrites the last entry in the Vector and will never hold more than 2 elements ... I thought a Vector would grow and I have even used reserve to set it up larger than 2. I am doing something stupid, please point me in the right direction.

Code:

```for (int i=0; i<edge_cnt; i++) {     file >> origin;     file >> dest; #ifdef DEBUG3   cout << "origin: " << origin << " dest: " << dest << endl; #endif     // undirected     // check for origin node in map     EDGE2 *edge1 = new EDGE2(origin, dest, 0);     EDGE2 *edge2 = new EDGE2(dest, origin, 0);     map<int, vector<EDGE2*> >::iterator iii;     pair<int, vector<EDGE2*> > PAIR;     iii=graph->edgeMap.find(origin);     vector<EDGE2*> v;     v.reserve(edge_cnt);  // where edge_cnt = 10     if (iii == graph->edgeMap.end())     {       v.push_back(edge1);       map<int, vector<EDGE2*> >::value_type vt(origin, v);       graph->edgeMap.insert(vt); #ifdef DEBUG   cout << "Location " << origin << " not in map, inserting and adding edge with origin=" << edge1->origin <<           " dest=" << edge1->dest << endl; #endif     }     else     {       // get vector and add edge to it       PAIR=(*iii);       PAIR.second.push_back(edge1); #ifdef DEBUG   cout << "Location " << PAIR.first << " is in the map, adding an edge ..." << endl;   cout << "  This vector can hold " << v.capacity() << " elements up to " << v.max_size() << endl;   cout << "  Added edge with origin=" << edge1->origin << " dest=" << edge1->dest << endl;   cout << "  Location " << PAIR.first << " now has " << PAIR.second.size() << " edges ..." << endl;   vector<EDGE2*>::iterator eiii;   cout << "      Iterating vector ..." << endl;   cout << "      ====================" << endl;   for (eiii=PAIR.second.begin(); eiii!=PAIR.second.end(); eiii++)     cout << "      origin=" << (*eiii)->origin << " dest=" << (*eiii)->dest << endl; #endif     } }```
• 12-06-2002
geeoff99
max of 2 in vector in map<int, vector<EDGE*> > ??