开发者

How can I store an inventory-like list of numbers?

I've got a list of number that I need to keep track of. The numbers are loosely related, but represent distinctly different items. I'd like to keep a list of the numbers but be able to refer to them by name so that I can call them and use them where needed easily. Kind of like an inventory listing, where the numbers all refer to a part ID and I'd like to call them idPart1, idPart2, idPart3 so their purpose is easily identifiable when they are used.

What would be the best way to do this?

1)Define a structure. Say, Inventory. A number of int members will be included, part1, part2 etc. To use, an instance of the structure will be created, values assigned to the members, and the numbers will be used by saying struct.member as needed.

2)Define an enumeration. Use part1, part2 as the enum literals. Store the actual values in a vector or list, each one at the index corresponding to the value of the number's name within the enum. 开发者_如何学编程Use the enum literals to retrieve the values, list[enumLit].

3)Something completely different

There's nothing else I need to do with the numbers - just look them up every once in a while. Since there's no processing, I kind of think a new class for them is overkill, but I'm willing to be convinced otherwise.

Any suggestions?


Let me try to rephrase what you're trying to do here. You want developers who use your code to be able to refer to a pre-defined set of numeric values:

  • using intuitive names
  • that will be validated at compile time
  • and that the IDE will recognize for the sake of code completion.

If the values will not change at run-time, and they are integer values, you can use an enum as Mark Ransom showed.

If the values will not change at run-time, and they are non-integer values, you can use either #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 may change at run-time, you can use either of the two options you identified. Both of these options have the drawback of requiring an object to be instantiated which holds the current part number values. The other options are simpler to implement and don't require any run-time lookup. Everything is resolved at compile time.

All of these options require users of your code to recompile if they are to access new part types. Your first option may require existing code to be recompiled when new part types are added, even if the existing code doesn't access them; it's more prone to memory layout changes.


You can use a map with a string as the key.

std::map<string,int> mymap;
mymap["part1"] = value1;
cout << mymap["part1"];


You could use the:

std::map<string, int> someMapName;

with the key as the string and the actual number as the int. That way you could do you could use

someMapName["idPart1"]

to grab the number.'

EDIT: If you are ok with Enumerations then option 2 would work perfectly with the std::map just instead of string, the key would be your enum type obviously.


Based on your comments to the other answers, I'd say enums are the way to go, but I'd structure them a little differently.

namespace id {
    enum {
        part1 = 123,
        part2 = 456,
        part3 = 987,
        ...
    };
}

cout << id::part1;


Use a database.
Specifically, a table like the following:

+------------------+-------------------+
| Item Name        | Item Number       |
+------------------+-------------------+

Internally, this can be represented as:

std::map<std::string, // The item name
        unsigned int> // The number.

When you want the number, retrieve 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜