Thread: help with Map and Vector

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    Unhappy 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
        }
    }
    Last edited by geeoff99; 12-06-2002 at 12:55 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    max of 2 in vector in map<int, vector<EDGE*> > ??

    see additions below:
    ================

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of a struct containing another vector
    By Todd88 in forum C++ Programming
    Replies: 61
    Last Post: 12-05-2008, 06:38 PM
  2. map or vector, which is less performant and by how much?
    By elninio in forum C++ Programming
    Replies: 14
    Last Post: 09-23-2008, 08:49 AM
  3. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 05:01 AM
  4. vector <string> in a map
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2006, 10:34 AM
  5. 2D colision with vector map
    By PsychoBrat in forum Game Programming
    Replies: 18
    Last Post: 08-16-2003, 12:38 PM