Question on using <map> in C++

  • C/C++
  • Thread starter CFDFEAGURU
  • Start date
  • Tags
    C++ Map
In summary, 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.
  • #1
CFDFEAGURU
783
10
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
 
Technology news on Phys.org
  • #2
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
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
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
Thanks for your help so far.

jtbell,

Could you use a multimap for this application?

Thanks
Matt
 
  • #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.
 
  • #7
CFDFEAGURU said:
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
Borek said:
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
I have it all working correctly. I used the multimap function and it works perfect.

Thanks for all of you help.

Matt
 

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

1. What is a in C++?

A in C++ is a data structure that stores key-value pairs. It allows for efficient lookup and retrieval of values based on a corresponding key.

2. How do you declare and initialize a in C++?

A in C++ can be declared and initialized using the following syntax:
std::map map_name;
map_name[key] = value;

3. Can a contain duplicate keys?

No, a in C++ cannot contain duplicate keys. Each key must be unique, but the corresponding values can be duplicated.

4. How do you search for a specific key or value in a in C++?

To search for a specific key in a , you can use the find() function which returns an iterator pointing to the element if found, or to the end of the map if not found. To search for a specific value, you can use the count() function which returns the number of elements with the specified value.

5. How do you delete a specific key-value pair from a in C++?

To delete a specific key-value pair from a , you can use the erase() function and pass in the key of the element you want to delete.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
3
Views
415
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top