Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question on using <map> in C++

  1. May 1, 2010 #1
    Hello all,

    I am using 2008 C++ Express edition on a Windows XP machine and I have the following question regarding use of a map.

    How would you use a map to do the following:

    The user enters a pipe size, say 1/8" NPS, now there are three possible schedules for that pipe size. They are 10S, 40S Std, 80S Ex Hvy.

    So once the pipe size of 1/8" NPS is entered by the user the three schedules are then printed for the user to select from.

    Note: This is just a simplified example, in the real program there are many sizes of pipe to choose from and many schedules.

    Any help would be greatly appreciated.

  2. jcsd
  3. May 1, 2010 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You wouldn't.

    You would use a map to store a collection of key-value pairs, in a way that makes it easy to, if you have a key, to find the corresponding value.

    This ability may be of use in a program for solving your problem -- but a map cannot solve your problem all by itself.
  4. May 1, 2010 #3


    User Avatar

    Staff: Mentor

    To make this more explicit: in a map, each key has exactly one value associated with it. Your example has three values (schedules) associated with one key (1/8" NPS).
  5. May 1, 2010 #4


    User Avatar

    Staff: Mentor

    You can always try to map keys to arrays (or lists, or whatever). No idea about implementation details, but I am sure it is doable.
  6. May 1, 2010 #5
    Thanks for your help so far.


    Could you use a multimap for this application?

  7. May 1, 2010 #6
    You could use struct(ures) or classes or structures of classes to hold the data and display in pop-up windows or drop-down menus. That would require you to establish a library that contains every possible choice that could be made. You would have to build the library file (database) and then call from it for your menus or windows. The data selected could then be placed into a structure or class for use in whatever you are going to do with it.
    If you don't want to have a database then you could do the same thing without the library file and let the user select size and schedule from experience or manuals.
  8. May 1, 2010 #7


    User Avatar

    Staff: Mentor

    I've never used a multimap myself, but after looking at what Stroustrup's book says about it, I'd say it looks perfect for your application. You need to be acquainted with using iterators, because you have to use an iterator to pull out all the values (schedules) that correspond to a given key.
    Last edited: May 1, 2010
  9. May 1, 2010 #8
    You can't use raw arrays since they aren't copyable as required by STL containers. You'd have to use std::tr1::array, boost::array or a std::vector.
  10. May 2, 2010 #9
    I have it all working correctly. I used the multimap function and it works perfect.

    Thanks for all of you help.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook