【C++】stringで取得した文字列をcharに変換?する
charは扱いなれていないため、メモ。
変換というよりは、別の変数に詰めてるだけです。
普段データ分析であったり数値を扱うことがほとんどなので、charに苦手意識があったのですが、まさかこんなことで躓くとは...。
より精進しないといけないと反省しました。
コード
#include <iostream> #include <string.h> using namespace std; int main(){ int s_x; int i = 0; string x = "JAPAN"; const char *X = NULL; X = x.c_str(); //string:xをchar:Xに s_x = x.length(); //xの文字数を取得 while(i < s_x){ cout << X[i] << endl; i++; } return 0; }
出力は以下、
$ ./a.out J A P A N