1

I want to generate some variables using for command. look at code below:

for (char ch='a'; ch<='z'; ch++)
      int ch=0;

It just an example, after running code above, I want to have int a, int b, int c ...

another example:

for (int i=0; i<10; i++)
      int NewiEnd=0;

For example after running code above, we will have int New1End, int New2End etc.

Hope I'm clear enough, How can I do such thing in C++??

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Inside Man
  • 4,194
  • 12
  • 59
  • 119

4 Answers4

7

No, not possible, not exactly. However, this is possible:

std::map<char,int> vars;    
for (char ch='a'; ch<='z'; ch++)
      vars[ch] = 0;

std::cout << vars['a'] << vars['b'] << vars['c'];

You can also have std::map<std::string, int>.

std::map<std::string,int> vars;
for (int i=0; i<10; i++)
    vars["New" + std::to_string(i) + "End"] = 0;
std::cout << vars["New5End"];
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • thanks for reply, but Which header should I use for using map command? – Inside Man Nov 04 '12 at 17:47
  • @Stranger: `` -- And it's not a command, it's a class template. [Here's some unofficial documentation.](http://en.cppreference.com/w/cpp/container/map) – Benjamin Lindley Nov 04 '12 at 17:49
  • What should I write If I want to use map? I have written the code above but it is not working, should I add the whole class template of map into my source code? – Inside Man Nov 04 '12 at 17:52
  • 1
    @Stranger Just put `#include `. If this doesn't work, show us your error. – Joseph Mansfield Nov 04 '12 at 17:52
  • Ok, I have tested it and it is working now, but it is not what really I'm looking for, because I must use Name["TargetItem"] for reaching my variable, for example I need Varsa not Vars['a']. So is there any way to fix it too? – Inside Man Nov 04 '12 at 18:03
  • 2
    @Stranger: No, that is absolutely not possible with C++, as we have been telling you. If you need that so bad, then you are using the wrong language. Try python, where it is possible. – Benjamin Lindley Nov 04 '12 at 18:08
  • @BenjaminLindley "...you keep using that word. I don't think it mean what you think it mean." :) ["impossible"](http://stackoverflow.com/a/13221189/211160) – HostileFork says dont trust SE Nov 04 '12 at 20:11
4

What you're trying to do isn't possible in C or C++.

David G
  • 94,763
  • 41
  • 167
  • 253
  • So What should I do? I need to do such thing, I believe nothing is impossible :) – Inside Man Nov 04 '12 at 17:24
  • 2
    @Stranger Those are what arrays are for. – David G Nov 04 '12 at 17:26
  • 2
    @Stranger: Count to infinity and then we talk again about `nothing is impossible` :) – Nobody moving away from SE Nov 04 '12 at 17:28
  • @David just think I want to generate David1Stranger, David2Stranger etc, How can I enter numbers between them? I believe it is not a good way doing it by hands ;) – Inside Man Nov 04 '12 at 17:31
  • 1
    @Stranger: It seems you are trying to solve a problem and present only part of it to us. Maybe we could help you better when you present your whole problem and the part of your solution you are stuck with. – Nobody moving away from SE Nov 04 '12 at 17:35
  • 1
    @Nobody - counting to infinity is a well-known problem for internet router protocols, and it's solved by defining infinity as a small finite value. – Pete Becker Nov 04 '12 at 17:49
  • 1
    @PeterBecker: Well then let me define that I am right :) It was just an example to show that there _are_ things that are impossible. – Nobody moving away from SE Nov 04 '12 at 17:51
  • Mathematicians count to infinity all the time. In fact, they know the [relative size of infinities](http://en.wikipedia.org/wiki/Aleph_number) and can tell you when one is bigger or smaller than another. Also, see [my answer](http://stackoverflow.com/a/13221189/211160). – HostileFork says dont trust SE Nov 04 '12 at 17:57
  • @HostileFork: There seems to be a misunderstanding of counting. I do not know of anyone (except Chuck Norris) who was able to count to infinity. Of course it is possible to work with infinity as mathematicians do but they do not count to infinity one by one. – Nobody moving away from SE Nov 04 '12 at 18:00
  • @Nobody you didn't say "one by one", although that in itself would have to be formally defined before being called impossible within said formalism... – HostileFork says dont trust SE Nov 04 '12 at 19:58
  • @HostileFork: Lets end this here I think the purpose of my initial comment was clear. I surrender to the mathematical correctness :) Just in case you were getting this wrong: My initial comment was never intended to sound that harsh like it does in retrospective. I just wanted to give a short example that showed the limitations we all have to face. Now I see I had better chosen an example that was closer to the problem like: You cannot name a variable `*` or so. So once again sorry for the inconvenience. – Nobody moving away from SE Nov 05 '12 at 14:01
4

What you seem to want is a map of the type:

std::map<std::string, int> ints;

This will let you call "variables" by name:

ints["a"] = 0;
ints["myVariable"] = 10;

Or as given in your example:

std::map<char, int> ints;
for (char ch='a'; ch<='z'; ch++)
   ints[ch] = 0;

If you are just about to use 'a' - 'z' you could use an array of ints:

int ints['z' + 1];
ints['a'] = 0;
ints['z'] = 0;

But this allocates unnecessary space for the ascii characters below 'a'.

  • As well as unnecessary space in the middle for some encodings. Not all the world is ASCII. – Pete Becker Nov 04 '12 at 17:49
  • @PeteBecker: I never though about it but is there no line in the standard that says: convert char literals like 'a' to integers using ASCII? – Nobody moving away from SE Nov 04 '12 at 18:03
  • There's one specific requirement about encodings for characters: the code points for `'0'` through `'9'` must be contiguous and increasing, so that you can say `ch - '0'` to get the value of a character that represents one of those digits. – Pete Becker Nov 04 '12 at 20:27
  • Character constants are just integral constants. `'a'` is a number, just as `97` is a number. Its value is determined by the encoding (ASCII, EBCDIC, whatever) of the source character set. – Pete Becker Nov 04 '12 at 20:30
2

In C/C++ the variable names have "gone away" by the time the code has been compiled and run. You can't print out the name of an existing variable at run time via "reflection"...much less make new named variables. People looking for this feature find out that the only generalized way you can do it falls down to using the preprocessor:

generic way to print out variable name in c++

The preprocessor could theoretically be applied to your problem as well, with certain constraints:

Writing a while loop in the C preprocessor

But anyone reading your code would probably drive a stake through your heart, and be justified in doing so. Both Sunday-morning laziness and a strong belief that it's not what you (should) want leads me to not try and write a working example. :-)

(For the curious, the preprocessor is not Turing-Complete, although there are some "interesting" experiments)

The nature of C/C++ is to have you build up named tables on an as-needed basis. The languages that offer this feature by default make you pay for the runtime tracking of names whether you wind up using reflection or not, and that's not in the spirit of this particular compiled language. Others have given you answers that are more on the right track.

Community
  • 1
  • 1