如何存储类似库存的数字列表?
我有一个需要跟踪的号码列表。这些数字松散相关,但代表明显不同的项目。我想保留一个号码列表,但能够通过名称来引用它们,以便我可以轻松地调用它们并在需要时使用它们。有点像库存清单,其中的数字都指零件 ID,我想将它们称为 idPart1、idPart2、idPart3,这样在使用时可以轻松识别它们的用途。
最好的方法是什么?
1)定义一个结构体。说,库存。将包含许多 int 成员,part1、part2 等。使用时,将创建结构的实例,将值分配给成员,并根据需要通过说出 struct.member 来使用数字。
2)定义一个枚举。使用part1、part2作为枚举文字。将实际值存储在向量或列表中,每个值都位于与枚举中数字名称的值相对应的索引处。使用枚举文字来检索值,list[enumLit]。
3)完全不同的东西
我不需要对这些数字做任何其他事情 - 只需偶尔查找它们即可。由于没有处理,我有点认为对他们来说一个新的类是多余的,但我愿意相信事实并非如此。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
让我尝试重新表述一下您在这里想要做的事情。您希望使用您的代码的开发人员能够引用一组预定义的数值:
如果这些值在运行时不会改变,并且它们是整数值,则可以使用枚举,如 Mark Ransom 所示。
如果这些值在运行时不会更改,并且它们是非整数值,则可以使用 #define 或 const 变量:
或者
如果这些值可能在运行时更改,则可以使用您确定的两个选项之一。这两个选项都有一个缺点,即需要实例化一个保存当前零件编号值的对象。其他选项更容易实现,并且不需要任何运行时查找。一切都在编译时解决。
如果代码的用户要访问新的部件类型,所有这些选项都需要重新编译。您的第一个选项可能需要在添加新部件类型时重新编译现有代码,即使现有代码无法访问它们;它更容易发生内存布局变化。
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:
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:
or
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.
您可以使用:
将键作为字符串,将实际数字作为 int。这样你就可以用它
来抢号码。
编辑:
如果您对枚举没问题,那么选项 2 将与 std::map 完美配合,而不是字符串,关键显然是您的枚举类型。
You could use the:
with the key as the string and the actual number as the int. That way you could do you could use
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.
使用数据库。
具体来说,如下表:
在内部,这可以表示为:
当您需要号码时,使用名称检索它:
说真的,使用数据库。查看 SQLite 和 MySQL。
Use a database.
Specifically, a table like the following:
Internally, this can be represented as:
When you want the number, retrieve it using the name:
Seriously, use a database. Check out SQLite and MySQL.