Loose-Info.com
Last Update 2023/07/27
TOP - 各種テスト - C++ - 文字集合

概要

現在の環境における、基本文字集合(文字セット)の文字コードリスト(実装依存)を出力
代替トークン(alternative token)の使用


現在の環境における、基本文字集合(文字セット)の文字コードリスト(実装依存)を出力


sample.cpp
#include <iostream> #include <string> #include <iomanip> void func_cout(char c, std::string s) { int x = std::char_traits<char>::to_char_type(c); std::cout << s << std::setw(2) << std::setfill('0') << x << std::endl; } int main() { /* アルファベット */ char c_al[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; /* 10進数字 */ char c_10[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; /* 記号文字 */ char c_gr[] = { '!', '"', '#', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '[', '\\', ']', '^', '_', '{', '|', '}', '~' }; std::cout << "********** 文字集合の文字コードリスト(実装依存) **********\n" << std::endl; std::size_t i; std::cout << "********** アルファベット **********" << std::endl; std::cout << std::uppercase << std::hex; for (i=0; i<sizeof(c_al); i++) { int x = std::char_traits<char>::to_char_type(c_al[i]); std::cout << c_al[i] << " --- " << x << "\t"; if ((i % 4) == 3) { std::cout << std::endl; } } std::cout << "\n********** 10進数字 **********" << std::endl; for (i=0; i<sizeof(c_10); i++) { int x = std::char_traits<char>::to_char_type(c_10[i]); std::cout << c_10[i] << " --- " << x << "\t"; if ((i % 5) == 4) { std::cout << std::endl; } } std::cout << "\n********** 記号文字 **********" << std::endl; for (i=0; i<sizeof(c_gr); i++) { int x = std::char_traits<char>::to_char_type(c_gr[i]); std::cout << c_gr[i] << " --- " << x << "\t"; if ((i % 5) == 4) { std::cout << std::endl; } } std::cout << "\n\n********** 空白・制御文字 **********\n" << std::endl; func_cout(' ', " (空白) --- "); func_cout('\t', "\\t (水平タブ) --- "); func_cout('\v', "\\v (垂直タブ) --- "); func_cout('\n', "\\n (改行) --- "); func_cout('\f', "\\f (改ページ) --- "); func_cout('\a', "\\a (アラート) --- "); func_cout('\b', "\\b (バックスペース) --- "); func_cout('\r', "\\r (復帰) --- "); func_cout('\0', "\\0 (null文字) --- "); std::cout << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out ********** 文字集合の文字コードリスト(実装依存) ********** ********** アルファベット ********** A --- 41 B --- 42 C --- 43 D --- 44 E --- 45 F --- 46 G --- 47 H --- 48 I --- 49 J --- 4A K --- 4B L --- 4C M --- 4D N --- 4E O --- 4F P --- 50 Q --- 51 R --- 52 S --- 53 T --- 54 U --- 55 V --- 56 W --- 57 X --- 58 Y --- 59 Z --- 5A a --- 61 b --- 62 c --- 63 d --- 64 e --- 65 f --- 66 g --- 67 h --- 68 i --- 69 j --- 6A k --- 6B l --- 6C m --- 6D n --- 6E o --- 6F p --- 70 q --- 71 r --- 72 s --- 73 t --- 74 u --- 75 v --- 76 w --- 77 x --- 78 y --- 79 z --- 7A ********** 10進数字 ********** 0 --- 30 1 --- 31 2 --- 32 3 --- 33 4 --- 34 5 --- 35 6 --- 36 7 --- 37 8 --- 38 9 --- 39 ********** 記号文字 ********** ! --- 21 " --- 22 # --- 23 % --- 25 & --- 26 ' --- 27 ( --- 28 ) --- 29 * --- 2A + --- 2B , --- 2C - --- 2D . --- 2E / --- 2F : --- 3A ; --- 3B < --- 3C = --- 3D > --- 3E ? --- 3F [ --- 5B \ --- 5C ] --- 5D ^ --- 5E _ --- 5F { --- 7B | --- 7C } --- 7D ~ --- 7E ********** 空白・制御文字 ********** (空白) --- 20 \t (水平タブ) --- 09 \v (垂直タブ) --- 0B \n (改行) --- 0A \f (改ページ) --- 0C \a (アラート) --- 07 \b (バックスペース) --- 08 \r (復帰) --- 0D \0 (null文字) --- 00

代替トークン(alternative token)の使用


sample.cpp
// #include %:include <iostream> %:include <string> // #define %:define SAMP_MACRO1(a, b) %:a " " %:b // #a " " #b %:define SAMP_MACRO2(a, b) a %:%: b // a ## b %:define SAMP_MACRO3 "samp_macro3" void samp_func() <% // { std::cout << "samp_func() <% %>" << std::endl; %> // } int main() { samp_func(); // int n[3] = {1, 2, 3} int n<:3:> = <%1, 2, 3%>; for (int i=0; i<3; i++) { std::cout << "n[" << i << "]=" << n<:i:> << std::endl; } std::cout << SAMP_MACRO1(aaa, bbb) << std::endl; std::cout << SAMP_MACRO2(SAMP_, MACRO3) << std::endl; int x = 1; int y = 2; int j; if ((x == 1) and (y == 2)) // (x == 1) && (y == 2) { std::cout << "(x == 1) and (y == 2)" << std::endl; } if ((x > 1) or (y == 2)) // (x > 1) || (y == 2) { std::cout << "(x > 1) or (y == 2)" << std::endl; } /* ここ以降の出力は16進数(大文字) */ std::cout << std::uppercase << std::hex; // ~ ---> compl std::cout << "~ 0x00000000 = " << ~ 0x00000000 << std::endl; std::cout << "compl 0x00000000 = " << compl 0x00000000 << std::endl; // | ---> bitor std::cout << "(0x71 | 0x5F) = " << (0x71 | 0x5F) << std::endl; std::cout << "(0x71 bitor 0x5F) = " << (0x71 bitor 0x5F) << std::endl; // ^ ---> xor std::cout << "(0x71 ^ 0x5F) = " << (0x71 ^ 0x5F) << std::endl; std::cout << "(0x71 xor 0x5F) = " << (0x71 xor 0x5F) << std::endl; // & ---> bitand std::cout << "(0x71 & 0x5F) = " << (0x71 & 0x5F) << std::endl; std::cout << "(0x71 bitand 0x5F) = " << (0x71 bitand 0x5F) << std::endl; // ! ---> not std::cout << "! 0 = " << ! 0 << " ! 1 = " << ! 1 << std::endl; std::cout << "not 0 = " << not 0 << " not 1 = " << not 1 << std::endl; // != ---> not_eq std::cout << "(x != y) = " << (x != y) << " (x != 1) = " << (x != 1) << std::endl; std::cout << "(x not_eq y) = " << (x not_eq y) << " (x not_eq 1) = " << (x not_eq 1) << std::endl; // &= ---> and_eq j = 0x0F; j &= 0x15; std::cout << "j = 0x0F; j &= 0x15 ---> j = " << j << std::endl; j = 0x0F; j and_eq 0x15; std::cout << "j = 0x0F; j and_eq 0x15 ---> j = " << j << std::endl; // ^= ---> xor_eq j = 0x0F; j ^= 0x15; std::cout << "j = 0x0F; j ^= 0x15 ---> j = " << j << std::endl; j = 0x0F; j xor_eq 0x15; std::cout << "j = 0x0F; j xor_eq 0x15 ---> j = " << j << std::endl; // |= ---> or_eq j = 0x0F; j |= 0x15; std::cout << "j = 0x0F; j |= 0x15 ---> j = " << j << std::endl; j = 0x0F; j or_eq 0x15; std::cout << "j = 0x0F; j or_eq 0x15 ---> j = " << j << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out samp_func() <% %> n[0]=1 n[1]=2 n[2]=3 aaa bbb samp_macro3 (x == 1) and (y == 2) (x > 1) or (y == 2) ~ 0x00000000 = FFFFFFFF compl 0x00000000 = FFFFFFFF (0x71 | 0x5F) = 7F (0x71 bitor 0x5F) = 7F (0x71 ^ 0x5F) = 2E (0x71 xor 0x5F) = 2E (0x71 & 0x5F) = 51 (0x71 bitand 0x5F) = 51 ! 0 = 1 ! 1 = 0 not 0 = 1 not 1 = 0 (x != y) = 1 (x != 1) = 0 (x not_eq y) = 1 (x not_eq 1) = 0 j = 0x0F; j &= 0x15 ---> j = 5 j = 0x0F; j and_eq 0x15 ---> j = 5 j = 0x0F; j ^= 0x15 ---> j = 1A j = 0x0F; j xor_eq 0x15 ---> j = 1A j = 0x0F; j |= 0x15 ---> j = 1F j = 0x0F; j or_eq 0x15 ---> j = 1F

実行環境

GNU bash, version 5.1.16
GCC-12.2.0
GNU C Library 2.36
GNU Binutils 2.39


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。