inserting data in nested STL maps

This is a discussion on inserting data in nested STL maps within the C++ Programming forums, part of the General Programming Boards category; Code: std :: map <QString, std :: vector <std :: pair <QString, QString> > > configFileDataVector; The examples on Google ...

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    inserting data in nested STL maps

    Code:
    std :: map <QString, std :: vector <std :: pair <QString, QString> > > configFileDataVector;
    The examples on Google show the insertion techniques on non-nested maps, I tried this:
    Code:
    configFileDataVector.insert (std::make_pair ("fdsf", ("Peter Q.","5328")));
    It shows a warning:
    left-hand operand of comma has no effect
    It may be simple, but somehow nothing is striking me at this moment,
    Please guide.
    Last edited by AnishaKaul; 12-13-2010 at 05:25 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,293
    left-hand operand of comma has no effect
    that's because

    Code:
    ("Peter Q.","5328")
    is basically meaningless to the compiler.

    think about it. you are inserting into a std::map<QString, std::vector<std:: pair<QString, QString> > >. the value portion of the map is a std:: pair, so you need to insert it as such. the compiler cannot automatically deduce that ("Peter Q.","5328") is a std:: pair.

    you're very close, but you need to be explicit (like always) about what you're inserting as the value of the std:: pair.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks for replying,

    Code:
    configFileDataVector.insert (std::make_pair ("fdsf", std::make_pair ("Peter Q.","5328")));
    Is this what you mean? I can try that out tomorrow morning only!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,293
    yes, that's exactly what I mean. that should solve your problem.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    That wont quite work. The mapped type is a vector of pairs, not just a pair by itself. You'd need:
    Code:
    configFileDataVector.insert(std::make_pair("fdsf", std::vector(1, std::make_pair("Peter Q.", "5328"))));
    However, I'd suggest simply doing this instead:
    Code:
    configFileDataVector["fdsf"].push_back(std::make_pair("Peter Q.", "5328"));
    Note that this assumes that the key value doesn't currently map to a non-empty vector, or that you actually intend to insert multiple items into the vector in this manner.
    Last edited by iMalc; 12-13-2010 at 11:20 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,293
    Quote Originally Posted by iMalc View Post
    That wont quite work. The mapped type is a vector of pairs, not just a pair by itself.
    good catch... I didn't see the vector in there.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    typedefs are your friend when working with the STL. Using them will alleviate a lof of confusion.

  8. #8
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by iMalc View Post
    You'd need:
    Code:
    configFileDataVector.insert(std::make_pair("fdsf", std::vector(1, std::make_pair("Peter Q.", "5328"))));
    However, I'd suggest simply doing this instead:
    Code:
    configFileDataVector["fdsf"].push_back(std::make_pair("Peter Q.", "5328"));
    This did the trick, than you for bothering, this was simple but I probably was not putting much effort in figuring it out, thanks again. I followed your second way.

    I also got some error messages which I couldn't even read properly, leave alone understanding,

    Thanks to all others who responded.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  3. no tree data structure in STL
    By manav in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2008, 12:23 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Assign Character Arrays data from an STL Container
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2001, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21