0

How do you make a variable name where you create a variable and then in brackets the variable number? (By the way, I'm just guessing out how the code should be so that you get what I'm trying to say.) For example:

int var[5];
//create a variable var[5], but not var[4], var[3], var[2], etc.

Then, the variable number must be able to be accessed by a variable value:

int number = 5;
int var[number]; //creates a var[5], not a var[4], etc.
int var[2]; //creates a var[2], not a var[1], etc.
cout >>var[number];
number = 2;
cin << var[number];

If I'm way off track with my "example", please suggest something else. I need something similar to this for my game to operate, because I must be able to create an unlimited instance of bullets, but they will also be destroyed at one point.

Ripspace
  • 551
  • 6
  • 21
  • 1
    What is the point of using array at all when you don't want other elements in the sequence ? – Mahesh Dec 11 '11 at 04:10
  • 1
    What you want is unclear. `int var[5]` doesn't create a "`var[5]`". C/C++ uses zero-based indices, so it only creates an array of 5 integers indexed 0 to **4**. Also, if you only want one integer, then don't create an array. – Nicol Bolas Dec 11 '11 at 04:11
  • I don't understand this question. Why do you only want one element in the array? Why not just use one variable instead? – Marlon Dec 11 '11 at 04:12
  • Are you looking for a sparse array? – icktoofay Dec 11 '11 at 04:13
  • Are you trying to dynamically create variables with names like var_1, var_2, var_3, etc? – user973572 Dec 11 '11 at 04:14
  • I guess it looks like you want a map as @refp answered. what you mean by 'variable number' is actually a unique ID with which identify any single instance of a bullet? I strongly suggest that you try and learn C++ further, so you grasp its concepts a little better, it's a complicated language and you seem to be skipping some basic knowledge. – Petruza Dec 11 '11 at 04:30

3 Answers3

4

It looks like you are looking for the functionality provided by std::map which is a container used to map keys to values.

Documentation of std::map


Example use

In the below example we bind the value 123 to the integer key 4, and the value 321 to key 8. We then use a std::map<int,int>::const_iterator to iterate over the key/value pairs in our std::map named m.

  #include <map>

  ...

  std::map<int, int> m;

  m[4] = 123;
  m[8] = 321;

  for (std::map<int, int>::const_iterator cit = m.begin (); cit != m.end (); ++cit)
    std::cout << cit->first << " -> " << cit->second << std::endl;

output:

  4 -> 123
  8 -> 321
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • I tried your code and it worked! However, I used "int" for the example, but I intend on using user made variables like classes. How would I declare a class using your method? – Ripspace Dec 11 '11 at 04:57
  • @Ripspace If you'd like to use a *user defined object* as the **key** in your map you will have to define an `operator<` for that class, or provide `std::map` with a comparer as the third template argument. You can read more about it under [this SO-question](http://stackoverflow.com/questions/1102392/stl-maps-with-user-defined-objects) – Filip Roséen - refp Dec 11 '11 at 05:03
  • I've studied the post for half an hour and couldn't figure it out. What if I gave you a class "bullet"? Would you write an example? – Ripspace Dec 11 '11 at 05:27
  • @Ripspace you should be able to work it out from the examples of the linked thread, if you are having trouble the answer in [this thread](http://stackoverflow.com/questions/2214295/is-it-impossible-to-use-stl-map-with-struct) is as easy as it gets. – Filip Roséen - refp Dec 11 '11 at 05:31
1

It looks like you want variable length arrays, which is not something C++ supports. In most cases, the correct solution is to use an std::vector instead, as in

int number = 42; // or whatever
std::vector<int> var(number);

You can use std::vector as you would use an array in most cases, and you gain a lot of bonus functionality.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

If I understand what you want correctly (which I'm not certain that I do), you want to be able to create a place to hold objects and use them according to some index number, but to only create the specific objects which go in it on demand. You want do to this either because 1) you don't know how many objects you're going to create or 2) you aren't going to use every index number or 3) both.

If (1) then you should probably just use a vector, which is an array-like structure which grows automatically as you add more things to it. Look up std::vector.

If (2) then you could use an array of pointers and initially set all of the values to null and then use new to create the objects as needed. (Or you could use the solution recommend in part 3.)

If (3) then you want to use some form of map or hash table. These structures will let you find things by number even when not all numbers are in use and will grow as needed. I would highly recommend a hash table, but in C++, there isn't one in the STL, so you have to build your own or find one in a third-party library. For ease, you can use std::map, which is part of the STL. It does basically the same thing, but is slower. Some C++ distributions also include std::hash_map. If it's available, that should be used instead because it will be faster than std::map.

Keith Irwin
  • 5,628
  • 22
  • 31