如何存储类似库存的数字列表?

发布于 2024-08-30 23:56:03 字数 480 浏览 5 评论 0原文

我有一个需要跟踪的号码列表。这些数字松散相关,但代表明显不同的项目。我想保留一个号码列表,但能够通过名称来引用它们,以便我可以轻松地调用它们并在需要时使用它们。有点像库存清单,其中的数字都指零件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱娆 2024-09-06 23:56:03

让我尝试重新表述一下您在这里想要做的事情。您希望使用您的代码的开发人员能够引用一组预定义的数值:

  • 使用直观的名称
  • ,这些名称将在编译时进行验证
  • ,并且 IDE 将识别这些名称以完成代码。

如果这些值在运行时不会改变,并且它们是整数值,则可以使用枚举,如 Mark Ransom 所示。

如果这些值在运行时不会更改,并且它们是非整数值,则可以使用 #define 或 const 变量:

#define PART1 1.3
#define PART2 "1233-456"

或者

namespace PartNumbers
{
  const double Part1 = 1.3;
  const char* Part2 = "123-456"
}

如果这些值可能在运行时更改,则可以使用您确定的两个选项之一。这两个选项都有一个缺点,即需要实例化一个保存当前零件编号值的对象。其他选项更容易实现,并且不需要任何运行时查找。一切都在编译时解决。

如果代码的用户要访问新的部件类型,所有这些选项都需要重新编译。您的第一个选项可能需要在添加新部件类型时重新编译现有代码,即使现有代码无法访问它们;它更容易发生内存布局变化。

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.

辞取 2024-09-06 23:56:03

您可以使用以字符串作为键的映射。

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

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

std::map<string,int> mymap;
mymap["part1"] = value1;
cout << mymap["part1"];
神经大条 2024-09-06 23:56:03

您可以使用:

std::map<string, int> someMapName;

将键作为字符串,将实际数字作为 int。这样你就可以用它

someMapName["idPart1"]

来抢号码。

编辑:
如果您对枚举没问题,那么选项 2 将与 std::map 完美配合,而不是字符串,关键显然是您的枚举类型。

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.

你的笑 2024-09-06 23:56:03

根据您对其他答案的评论,我认为枚举是可行的方法,但我的结构会略有不同。

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

cout << id::part1;

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;
少女情怀诗 2024-09-06 23:56:03

使用数据库。
具体来说,如下表:

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

在内部,这可以表示为:

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

当您需要号码时,使用名称检索它:

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];

说真的,使用数据库。查看 SQLite 和 MySQL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文