How can I store an inventory-like list of numbers?
I have a list of numbers that I need to track. The numbers are loosely coupled but represent completely different elements. I would like to keep a list of numbers, but be able to refer to them by name so that I can call them and use them when needed is easy. Kind of like an inventory list where all numbers refer to a part id and I would like to name them idPart1, idPart2, idPart3 so that their purpose is easily identifiable when used.
What would be the best way to do this?
1) Define the structure. Let's say inventory. Several members int, part1, part2, etc. will be added. To use an instance of the structure, the values assigned to the members will be created, and numbers will be used if the struct.member needs to be specified.
2) Define an enumeration. Use part1, part2 as enumeration literals. Store the actual values in a vector or list, each corresponding to an index corresponding to the value of the number's name in the enumeration. Use enumeration literals to get values, list [enumLit].
3) Something completely different
There is nothing I have to do with the numbers - just go through them every once in a while. Since there is no processing, I kind of think the new class is overkill for them, but I'm willing to be convinced otherwise.
Any suggestions?
a source to share
Let me try to rephrase what you are trying to do here. You want the developers who use your code to be able to reference a predefined set of numeric values:
- using intuitive names
- which will be checked at compile time
- and that the IDE will be recognized for code completion.
If the values do not change at runtime and they are integer values, you can use an enumeration as Mark Ransom showed.
If the values will not change at runtime, and they are non-integer values, you can use #define or const variables:
#define PART1 1.3
#define PART2 "1233-456"
or
namespace PartNumbers
{
const double Part1 = 1.3;
const char* Part2 = "123-456"
}
If the values are likely to change at runtime, you can use either of the two parameters you specify. Both of these options have the drawback of requiring an instance of an object that contains the current values of the part number. Other options are easier to implement and do not require any runtime lookup. Everything is resolved at compile time.
All of these options require users of your code to recompile if they need to access new types of parts. Your first option may require you to recompile existing code when adding new part types, even if the existing code does not have access to them; it is more prone to memory layout changes.
a source to share
You can use:
std::map<string, int> someMapName;
with key as string and actual number as int. So you could use
someMapName["idPart1"]
to capture the number. '
EDIT: If you are okay with Enumerations option 2 will work fine with std :: map and not string, the key will appear explicitly in your enum.
a source to share
Use a database.
Specifically, the table looks like this:
+------------------+-------------------+
| Item Name | Item Number |
+------------------+-------------------+
Internally, this can be represented as:
std::map<std::string, // The item name
unsigned int> // The number.
When you want a number, extract it using the name:
std::map<std::string, unsigned int> index_by_name;
//...
std::string part_name = "Part0123";
unsigned int part_number = 0;
part_number = index_by_name[name];
Seriously, use a database. Check out SQLite and MySQL.
a source to share