1

Alright so this is super hard to put in words. So I have some code:

vector <char> Chars;
for(char c = '0'; c <='z'; c++) {
    Chars.push_back(c);
}

And so it helps with adding characters to my character vector because it will add the select amount. Example:

vector <char> Chars;
for(char c = '0'; c <='9'; c++) {
    Chars.push_back(c);
}

Gets you 0123456789 and:

vector <char> Chars;
for(char c = '0'; c <='Z'; c++) {
    Chars.push_back(c);
}

Gets you 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ. How can I (a) get all of the characters and (b) what is the order of this? Why do they have some symbols after the numbers but not all of them?

MicroWither
  • 49
  • 1
  • 5
  • [ascii](http://en.cppreference.com/w/cpp/language/ascii), but I believe that C++ doesn't place too many requirements on the exact char encoding (i.e. doesn't have to be ascii). Although in practice, I don't know if there's any platform that wouldn't be ascii – Justin Jan 24 '18 at 21:50
  • @Justin: There is also [EBCDIC](https://en.wikipedia.org/wiki/EBCDIC) instead of Ascii (even if not really used). – Jarod42 Jan 24 '18 at 23:08
  • Since you are using literal characters (and of course correctly telling your compiler the source character encoding), the character encoding in question is whatever you are telling it to use as the execution character encoding (gcc [-fexec-charset](https://gcc.gnu.org/onlinedocs/cpp/Invocation.html); msvc:[/execution-charset](https://learn.microsoft.com/en-us/cpp/build/reference/execution-charset-set-execution-character-set) ). Neither is likely to be ASCII. Maybe UTF-8. Maybe Windows-1252. Whatever it is, you are in control and are creating a contract with your users. – Tom Blodget Jan 24 '18 at 23:54

3 Answers3

2

I think the following will be of help to you:

enter image description here

What that means is, if you start your loop from '0' character, that corresponds to number 48 in that table. You are simply incrementing from there and going up until 'Z'. If you want just the alphanumeric characters, just seperate your loop to two pieces.

SenselessCoder
  • 1,139
  • 12
  • 27
2

(b) what is the order of this?

It is the order specified in the native character encoding of the system that you use. It is probably one of ASCII, ISO/IEC 8859 or UTF-8 all of which are identical in the range [0, 128).

Why do they have some symbols after the numbers but not all of them?

Because some of the symbols are before the numbers, and some more are after the letters.

That's just the order that was chosen by whatever committee designed the encoding. There's not necessarily a deep philosophy behind that choice. It may be an anomaly inherited from teletype codes that preceded computer systems.

How can I (a) get all of the characters

You can use numeric limits to find the minimum and maximum values, and a loop to iterate over them:

for(int i = std::numeric_limits<char>::min();
        i < std::numeric_limits<char>::max(); i++) {
    char c = i;
    Chars.push_back(c);
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Each character has an ASCII code.

See https://en.wikipedia.org/wiki/ASCII

It just so happens that some symbols, but not all are located between '9' and 'A'.

As others have pointed out, it's probably not even guaranteed which symbols are where in that table. But I guess 'A' to 'Z', 'a' to 'z' and '0' to '9' are guaranteed to be contiguous.

Also, see: Does C and C++ guarantee the ASCII of [a-f] and [A-F] characters?

Jeffrey
  • 11,063
  • 1
  • 21
  • 42