Thread: STL Map Of Vectors

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    STL Map Of Vectors

    I want a map like this:

    map< string, vector<CMyClass> > m_mymap;

    Does anyone have any examples of inserting into a map like this, finding particular keys, getting the vector and adding to the existing vector, etc?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Using this map would not be different from many other maps. The insert function still works with make_pair(). Unless you know the key to a vector, you would use find(), and then dereference the resulting iterator to get to the vector, which can then be further accessed. Or is this your first time with maps?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Quote Originally Posted by whiteflags View Post
    Using this map would not be different from many other maps. The insert function still works with make_pair(). Unless you know the key to a vector, you would use find(), and then dereference the resulting iterator to get to the vector, which can then be further accessed. Or is this your first time with maps?
    first time with maps. I have been reading up but I was under the impression if I called find for a specific string key, I would just get a copy of the vector in the map. Therefore any push_back operation on the vector would not affect the vector in the map container. So this isn't the case?
    Last edited by Jonnster; 11-16-2010 at 04:50 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it is not the case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    I was expecting to do a find on the map, and with the iterator of this do:

    (*i)->push_back(data);

    This doesn't compile. So how would I push a new element to the vector?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the compile error, and also post the compile error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Code:
    map < string, vector <CMyClass> >::iterator i = m_MyMap.find("sometext");
    
    CMyClass data;
    // code to set data removed
    
    (*i)->push_back(data);
    I've removed the checks on the find for simplicity.

    The compiler error is

    Code:
    error C2819: type 'std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::vector<class CMyClass::CMyClass,class std::a
    llocator<class CMyClass::CMyClass> > >' does not have an overloaded member 'operator ->'
    error C2227: left of '->push_back' must point to class/struct/union
    Last edited by Jonnster; 11-16-2010 at 05:43 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. It should actually be:
    Code:
    i->second.push_back(data);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    It compiles now. Many thanks for your help.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What happens if find() doesn't find the key? Do you insert a new entry into the map and continue, or do you do something else.

    I ask because often it's better to assume the key is in there or add it automatically if it's not by using the operator[] syntax:
    Code:
    m_MyMap["sometext"].push_back(data);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 47
    Last Post: 07-13-2010, 07:22 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. STL Vectors
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2005, 08:35 PM
  4. STL Map Object
    By nomes in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2003, 01:51 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM