C++ 中对象的变量命名方案?

发布于 2024-07-07 17:00:05 字数 279 浏览 13 评论 0原文

我正在实现一个 BFS,它要做的是遍历有序树来找到难题的最短解决方案。

我要做的是创建一个快照对象,它保存拼图中每个部分的当前位置。 我会将这个 Snapshot 对象添加到队列中并检查它是否是解决方案。 然而,我正在即时创建这些快照。 那么是否有某种方法可以在将快照对象放入队列时自动生成快照对象的名称?

或者我是否必须跟踪我做了多少个声明,然后通过说...

Snapshot snapshot2; 对其进行硬编码? 快照快照3; 快照快照4; ETC..

I am implementing a BFS, and what it is going to do is go through an ordered tree to find the shortest solution to a puzzle.

What i will be doing is creating a Snapshot object that holds the current position of each piece in a puzzle. I will add this Snapshot object into the queue and check if it is the solution. However, I am creating these snapshots on the fly. So is there some kind of way that will automatically generate the names of the Snapshot objects when they are put into the queue?

or do i have to keep track of how many declarations i have made and just hard code it by saying...

Snapshot snapshot2;
Snapshot snapshot3;
Snapshot snapshot4;
etc..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

暖心男生 2024-07-14 17:00:05

我想您是在问,当您不知道会有多少对象时,如何创建和保留大量对象。

您需要创建一个对象数组。 然后您可以通过快照[1]、快照[2]...快照[i] 来访问它们。

I think you're asking how do you create and keep lots of objects when you don't know how many there will be.

You need to create an array of the objects. Then you can access them as snapshot[1], snapshot[2]... snapshot[i].

熟人话多 2024-07-14 17:00:05

有一种方法 - 您使用 预处理器的令牌粘贴运算符。 这允许您基于变量创建名称,因此您可以指定:

#define S(variable) snapshot#variable

并且您可以创建名为 snapshot1、snapshot2 等的变量:

Snapshot S(1)
Snapshot S(2)

但是,我不确定这是否是您真正想要的,我已经也许在实践中使用过一次这种技术,那是为了代码生成器。

There is a way - you use the Preprocessor's Token-Pasting Operator. This allows you to create a name based on a variable, so you'd specify:

#define S(variable) snapshot#variable

and you'd be able to create variables named snapshot1, snapshot2 etc:

Snapshot S(1)
Snapshot S(2)

However, I'm not sure this is what you really want, I've maybe used this technique once in practise, and that was for a code generator.

一袭白衣梦中忆 2024-07-14 17:00:05

您可以使用标准模板库中的队列,然后创建一个函数来创建快照对象并将其放入队列中。 为该函数提供一个静态变量,每次调用该函数时该变量都会递增并写入快照的 id 字段。

http://www.csci.csusb.edu/dick/samples/stl。 html

http://www.cppreference.com/wiki/stl/queue /开始

You could use a queue from the standard template library, then create a function that creates a Snapshot object and puts in into the queue. Give this function a static variable which gets incremented every time it is called and written into an id field of the snapshot.

http://www.csci.csusb.edu/dick/samples/stl.html

http://www.cppreference.com/wiki/stl/queue/start

淤浪 2024-07-14 17:00:05

我认为我们需要更多信息。 如果您只是将它们从队列中弹出,为什么要关心它们的名称呢? 队列中的对象通常不会编号,除非您在数组中实现它。

I think we need more information for this. If you're simply popping these out of a queue, why do you care what they are named? Objects in a queue are not normally numbered, unless you're implementing it in an array.

枕梦 2024-07-14 17:00:05

抱歉,整个队列的事情有点造成不必要的混乱。

我们再举一个例子。 因此对于这个拼图,拼图中的块数由用户指定。 我设计程序的方式是,拼图的每一块都是它自己的对象。

因此,当我开始创建这些片段时,我可以使用某种变量命名方案来命名这些片段。 所以像这样的事情只是作为一个例子......

for (int i-0; i < constraint; i++)
Piece "Piece"+i = new Piece();

Sorry, the whole queue thing kinda causes uneeded confusion.

Let's take another example. So for this puzzle, the number of pieces in the puzzle are specified by the user. The way I am designing the program is that each Piece of the puzzle is it's own object.

So when I go about creating these Pieces, can I use some kind of variable naming schemes to go about naming these Pieces. so something like this just as an example...

for (int i-0; i < constraint; i++)
Piece "Piece"+i = new Piece();
听不够的曲调 2024-07-14 17:00:05

你不能在 C++ 中动态创建变量名,至少在没有一些(想象的?)附加组件的情况下是这样。

编辑:顺便说一句,我做了一个作业,我认为它与你在人工智能课程中的作业类似,其中我们涵盖了 BFS、DFS 和 A* 等基础知识。 没有一次需要为“快照”拥有唯一命名的对象,并且我使用了队列。

edit2:如果您需要跟踪拥有多少个快照,请创建一个计数变量,该变量在每次创建对象时都会递增。

You cannot dynamically create variable names in C++, at least not without some (imaginary?) add-on.

edit: As an aside, I did an assignment that I assume is similar to yours in an AI class, where we covered basics like BFS, DFS, and A*. Not once was it necessary to have uniquely named objects for the "snapshots", and I used queues.

edit2: and if you need to keep track of how many Snapshots you have, create a count variable that increments every time you create the object.

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