Question on using <map> in C++

  • C/++/#
  • Thread starter CFDFEAGURU
  • Start date
  • #1
783
9

Main Question or Discussion Point

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.

Thanks
Matt
 

Answers and Replies

  • #2
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
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.
 
  • #3
jtbell
Mentor
15,584
3,561
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).
 
  • #4
Borek
Mentor
28,476
2,873
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.
 
  • #5
783
9
Thanks for your help so far.

jtbell,

Could you use a multimap for this application?

Thanks
Matt
 
  • #6
151
0
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.
 
  • #7
jtbell
Mentor
15,584
3,561
Could you use a multimap for this application?
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:
  • #8
20
0
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.
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.
 
  • #9
783
9
I have it all working correctly. I used the multimap function and it works perfect.

Thanks for all of you help.

Matt
 

Related Threads on Question on using <map> in C++

  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
2
Views
14K
  • Last Post
Replies
20
Views
9K
Replies
1
Views
426
Replies
1
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
13
Views
2K
Replies
3
Views
488
  • Last Post
Replies
2
Views
5K
Replies
2
Views
3K
Top