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++??

5
  • 4
    No. Why would you need this? There are containers. Commented Nov 4, 2012 at 17:23
  • 1
    Note that there is no requirement that the numeric values from a to z represent the letters a-z and nothing else. For example, in the EBCDIC encoding, there's quite a bit of punctuation stuck in the middle of that range of character codes. Commented Nov 4, 2012 at 17:48
  • @PeteBecker, I was in the middle of typing that :p Commented Nov 4, 2012 at 17:48
  • I think EBCDIC can be safely ignored by most people. Commented Nov 4, 2012 at 17:56
  • @BenjaminLindley: I wish I could ignore it :( Commented Nov 4, 2012 at 18:29

4 Answers 4

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"];
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for reply, but Which header should I use for using map command?
@Stranger: <map> -- And it's not a command, it's a class template. Here's some unofficial documentation.
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?
@Stranger Just put #include <map>. If this doesn't work, show us your error.
@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.
|
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'.

4 Comments

As well as unnecessary space in the middle for some encodings. Not all the world is ASCII.
@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?
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.
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.
3

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

11 Comments

@Stranger Those are what arrays are for.
@Stranger: Count to infinity and then we talk again about nothing is impossible :)
@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 - counting to infinity is a well-known problem for internet router protocols, and it's solved by defining infinity as a small finite value.
@PeterBecker: Well then let me define that I am right :) It was just an example to show that there are things that are impossible.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.