[C++] 25 string, char*, int間の変換

メインブログのTIPS記事はこちらに移動させます。

<string型からchar*型>
using std::string;

string str;
char* str_to_char = str.c_str();

<string型からchar[]型>
string str;

char ch[2048] = {};
str.copy(ch, 2047); // 最後にヌル文字が残るようにする

<int型からstring型>
using std::string; using std::to_string;

int num ;
string int_to_string = to_string(num);

<string型からint型>
using std::string; using std::stoi;

string str;
int str_to_int = stoi(str);

<char*型からint型> atoi関数はエラー時に0を返すので取り扱い注意!!
#include <stdlib.h>

char* ch;
int char_to_int = atoi(ch);

<int型からchar*型>
int型からstring型、string型からchar*型へ順に変換

using std::string; using std::to_string;

int num ;
string int_to_string = to_string(num);
char* str_to_char = (to_string(num)).c_str();

<const char*からstring型>
using std::string;

const char* cstr;
string cstr_to_string = string(cstr);

<char型単体からstring型>
char ch = 'a';
string str{ch}; strがstring型変数

<char[]型からstring型>
char chars[] = {'a', 'b', 'c'};
std::string str(chars, 3); // 3はcharの個数、strがstring型変数

[C++] size_t型

未だになじまないのでメモ。

長さ、大きさ、サイズを表現する型。sizeof演算子、strlen関数など、byte数や長さの表現に使用する。

unsigned longと同義。

[C++] コンパイルエラー std::vector

std::vectorはC03++以前の古いコンパイラでは対応していないため、コンパイル時にオプションとして-std=c++11などを追加します。

error: non-aggregate type 'std::vector<string>' \
(aka 'vector<basic_string<char, char_traits<char>, allocator<char> > >') \
cannot be initialized with an initializer list
std::vector<string> dakuon = {"が","ぎ"};

[VSCode] プロジェクト作成時のGit設定 非Xcode環境での開発

1.プロジェクトディレクトリを作成する。
2.カレントディレクトリをプロジェクトディレクトリに移動させる。
3. git initコマンドを実行し、ローカルリポジトリを初期化する。
4.VSCodeでプロジェクトディレクトリを開き、コーディングを開始。
5.適当なところで左のソース管理ボタンを押し、コミットする。

推奨拡張機能
Git Graph : バージョン管理状況を図で表示する。

[FLTK] macOS Montereyにアップグレードするとmakeできなくなった

[M1 Mac, Monterey 12.3.1, FLTK 1.3.8] 

macOSをBig SurからMonterey 12.3.1にアップグレードしたところ、makeができなくなりました。

エラー内容は以下の通りです。

$ make all
rm -rf ./obj/FileFinder.o ./bin/FileFinder FileFinder.app
clang++ -I/usr/local/include -I/usr/local/include/FL/images -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_THREAD_SAFE -D_REENTRANT -std=c++11   -I./include -I/usr/local/include/FL -g -o obj/FileFinder.o -c src/FileFinder.cpp
clang: warning: no such sysroot directory: '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk' [-Wmissing-sysroot]
In file included from src/FileFinder.cpp:1:
In file included from ./include/modalDialog.h:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:506:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string_view:175:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__string:57:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:641:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:60:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:60:15: fatal error: 'string.h' file not found
#include_next <string.h>

MacOSX12.1.sdkが見つからないために暴走しているようです。

正しくはMacOSX12.3.sdkなのですが、fltk-configが生成するオプション-isysrootがなぜか12.1を指定します。

仕方ないのでMakefileに以下のオプションを追記しました。これで正常にコンパイル&ビルドできます。

# 追加オプション(SDKのバージョンが間違っている場合。原因:fltk-configの不具合)
OPTION = -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk

<略>

# cppファイルからoファイル作成 $<:依存ファイル
$(OBJDIR)/FileFinder.o: $(SRCDIR)/FileFinder.cpp
	$(COMPILER) $(CPPFLAGS) $(CPPFLAGS2) $(INCLUDE) $(DEBUG) $(OPTION) -o $@ -c $<

[XCode] ファイル名の変更

ファイル名を変更するには、ファイル内のclass名あるいはstruct名を右クリックしてRefactor – Renameを選択します。

他ファイル内の同じ名前も勝手にRenameされてしまうので要注意です。

XCodeのクセの強さに困っています。左側のProject Navigatorから直接ファイル名を変えられないというのはあまりにも独特です。

ファイルを複製して内容を変えていくという方法を定番化させている身としてはこれらの機能は害悪でしかないです。

XCodeにはBuild & Runだけ任せましょうか。VSCodeでの編集内容はXCodeにすぐ反映されました。Gitも機能しているようです。XCodeは非表示にしてこれまで通りVSCodeとFinderでコーディングします。

XCodeはSwiftで開発するには他に選択肢がないので仕方なく使っています。SwiftのドキュメントはOracleのJavaに比べるとスカスカで今ひとつです。

XCodeやSwiftへの苦言を集めたら面白い読み物になりそうです。