views:

325

answers:

6

Hey everyone,

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..

A: 

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

da_code_monkey
but the problem is that when i create the snapshot objects and just use some kind of generic name likeSnapshot snapshot;And i add that to the queue. Now since i will be using a loop, its gonna declare it using the same name. Will it let me do that?
Tomek
If you declare it inside the loop, it should get re-created every iteration.
da_code_monkey
A: 

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.

ray
A: 

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();
Tomek
A: 

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.

ray
yea i thought about it some more, and your right its not necessary
Tomek
+4  A: 

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

WW
yea, sorry i didn't really know how to word that properly. thanks for the info
Tomek
A: 

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.

gbjbaanb