備忘録

プログラムやゲーム関連に関すること

【C++】標準コンテナでの値の削除

標準コンテナでの値の削除にはコンテナの種類に適した削除方法を選択する test.cpp #include <iostream> #include <iterator> #include <algorithm> #include <vector> #include <list> #include <map> #include <string> using namespace std; // 型の宣言 using my_map = map < string, int > ; using my_pair = pair </string></map></list></vector></algorithm></iterator></iostream>…

【C++】標準コンテナでの範囲処理

コンテナが持つ値をイテレータを使用してループ処理ではなく コンテナが持つメンバ関数の範囲処理で効率的に走査する test.cpp #include <iostream> #include <iterator> #include <vector> using namespace std; int main(int argc, char *argv[]) { auto show = [](const vector<int> &v) { </int></vector></iterator></iostream>…

【Lua】モジュール

モジュールを記載したファイルをファイル名のオブジェクト(モジュール) としてグローバル変数(_G)に設定し、パッケージに読み込みしたことを伝える require関数からファイルを読み込み、モジュールからオブジェクトを生成する 下記のモジュールの機能は…

【Lua】継承

継承には、メタテーブルである__indexフィールド(子)に親を設定する 子がメンバを呼び出したときにメタテーブルを検索してメンバがなければ 親の持つメタテーブルを参照する test.lua -->既定クラスを定義する Base = {} -->生成関数を定義する function B…

【Lua】プライベートメンバを持つクラス

ローカル内に自身のテーブル(メンバ)を定義して、 そのテーブルに対する公開メソッドとなるテーブル(インターフェイス)を返却する Luaのオブジェクトの基本設計ではプライベート機構は用意されていない これは、オブジェクトの汎用性(テーブル)を意図…

【Lua】行列

行列を多次元配列を使用して実装する test.lua -- 3x3の行列を生成する row = 3 -- 行 column = 3 -- 列 matrix = {} for i = 1, row do matrix[i] = {} for j = 1, column do matrix[i][j] = 0 end end test.cpp #include <iostream> #include <lua.hpp> using namespace std; </lua.hpp></iostream>…

【C++】クロージャ

Luaでクロージャを扱ったのでC++でも実装する クロージャに関することはLuaの記事を参照 参照:【Lua】クロージャ - 備忘録 test.cpp #include <iostream> #include <memory> #include <functional> int main(int argc, char *argv[]) { // 右辺の戻り値を型推論で省略すると // Visual C++</functional></memory></iostream>…

【Lua】クロージャ

以下の関数は呼び出し毎に値を加算して返却する 返却する値は無名関数であり、counter を呼び出し時に ローカル変数 i を定義してその値を無名関数内で参照している レキシカルスコープで非ローカル変数を参照する概念をクロージャという また、インスタンス…

【Lua】Tips

豆知識をこの記事に随時追加する。 ローカル変数のスコープについて グローバル変数とローカル変数へのアクセス速度は後者が速く、 スコープを抜けるとガベージコレクションが働きデータの保守性が上がる ローカル変数を使用する場合は必ずスコープの範囲で…

【Lua】C++環境の関数をLuaで使用する

参考:その5 LuaからC言語の関数を呼び出す test.cpp #include <iostream> #include <lua.hpp> void test() { // Luaステートを生成する auto L = luaL_newstate(); // Luaに登録する関数(線形補完)を定義する // Luaに登録できる関数は lua_CFunction と同じ型でなければな</lua.hpp></iostream>…

【Lua】LuaでC++のクラスを表現する

基本的なLuaライブラリは過去記事を参照。 参考:その4 会得必須!Luaの真髄「テーブル」 test.cpp #include <iostream> #include <lua.hpp> void test() { auto L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L, "test.lua")) { std::cout << lua_tostring(L, lua_</lua.hpp></iostream>…

【Lua】C++環境からコルーチンを実行する

test.cpp #include <iostream> #include <lua.hpp> void testCoroutine() { // Luaステートを生成する auto L = luaL_newstate(); // Luaステートに全てのライブラリを使用可能にする // 個別でライブラリを使用可能にするには luaL_requiref 関数を参照 luaL_openlibs(L); // L</lua.hpp></iostream>…

【Lua】コルーチン

Luaコード内でコルーチンを生成して実行する -- コルーチンを生成する -- ループ数を表示し、中断数を返却する local co = coroutine.create( function(x) -- 直接代入するので無名関数 for i = 0, x do print("loop count: " .. i) coroutine.yield("yield …

【Lua】基本処理

Luaの基本的な処理をLuaコードにてメモ -- 関係演算子 < > <= >= == ~= -- 論理演算子 and or not -- 単項演算子や三項演算子はない -- if 条件分岐 num = 10 if num <= 50 then print("under 50") elseif num <= 100 then print("under 100") else print("o…

【Lua】導入編 コマンドプロンプト

実行ファイルのダウンロード 下記URLから適当なものを選択する。(導入編 Visual Studioと同じ) 最新バージョンを選択し、Tools Executable をダウンロードする。 適当なディレクトリに解凍する。 解凍内容は、luaXX.exe、luacXX.exe、wluaXX.exe、luaXX.dl…

【Lua】スタック

スタックを把握する為に以下ソースコードをメモする。 詳しいことは、以下参考URLから参照。参考: その2 Luaスクリプト事始め #include <iostream> #include <lua.hpp> // スタック確認 void printStack(lua_State* L) { // スタック数を取得 const int num = lua_gettop(L); i</lua.hpp></iostream>…

【Lua】導入編 Visual Studio

下記URLから適当なものをダウンロードする。 当環境ではVC++での使用を目的とするので、VisualStudioと互換のある Visual C++ 2005 Compatible の最新バージョンである Windows Libraries をダウンロードする。 解凍先は、解りやすいようにC直下のluaフォル…

【C++】三角関数

基礎 引用:資格取得のための数学 画角からの焦点距離 #define _USE_MATH_DEFINES #include <iostream> #include <cmath> using namespace std; int main() { auto toRadian = [](float deg){ return deg * static_cast<float>(M_PI) / 180.f; }; auto width = 800.f; auto height = </float></cmath></iostream>…

【C++】多角形

R = m/2sin(180/n) n:多角形の辺(正n角形)の数 m:正多角形の辺の長さ R:求める円の半径引用:正多角形の描き方 #define _USE_MATH_DEFINES // M_PI #include <cmath> #include <iostream> using namespace std; int main() { // 度をラジアンに変換する </iostream></cmath>…

【C++】std::for_each での値の走査

ポインタでのイテレータの begin と end の割り当てに嵌ったのでメモ #include <iostream> #include <algorithm> #include <array> #include <iterator> int main() { // メモリ走査 int size = 4; int *i = new int[size]; memset(i, 0, sizeof(int) * size); std::for_each(&i[0], &i[size], [](i</iterator></array></algorithm></iostream>…

【C++】ストリーム

ストリームの階層 引用:Input/output library - cppreference.com ファイルモード std::ios::app // 追加出力 std::ios::ate // 開く時にEOFまで移動する std::ios::binary // ファイルをバイナリモードで開く std::ios::in // 読み込み専用で開く std::ios…

【C++】条件を満たす要素の数を取得

#include <iostream> #include <algorithm> int _tmain(int argc, _TCHAR* argv[]) { int i[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = std::count_if(std::begin(i), std::end(i), [](int x){ return x % 3 == 0; }); std::cout << n << std::endl; // 3 return 0; }</algorithm></iostream>

【C++】述語を評価する

#include <iostream> #include <algorithm> #include <array> int _tmain(int argc, _TCHAR* argv[]) { auto print = [](int x){ std::cout << x << std::endl; }; auto check = [](int x)->bool{ return x % 2 == 0; }; std::array<int, 4> array_0 = {0, 1, 2, 3}; print(std::all_of(array_0.c</int,></array></algorithm></iostream>…

【C++】ビット演算のラッパー

ビット演算をサポートするメンバ関数を内包 以下は一例 std::bitset<4> b0("0000"); std::bitset<4> b1("0101"); std::bitset<4> b2("1111"); auto print = [](bool x){ std::cout << x << std::endl; }; print(b0.all()); // 0 print(b0.any()); // 0 print…

【C++】関数オブジェクトのラッパー

std::function と std::bind について #include <iostream> #include <functional> // オブジェクトクラス class Object { public: void print(int x) const { std::cout << x << std::endl; } }; // グローバル空間に配置した関数 void print_gloval(int x) { std::cout << x << s</functional></iostream>…

【C++】否定を実行する関数オブジェクト

int 値の否定 int n = 1; std::cout << std::negate<int>()(n) << std::endl; // -1 int i[] = {1, -2, 3, -4}; std::transform(std::begin(i), std::end(i), i, std::negate<int>()); std::for_each(std::begin(i), std::end(i), [](int x){ std::cout << x << std::e</int></int>…

【C++】スマートポインタ補助関数

make_shared【C++11】 オブジェクトと管理用データを一緒に連続領域に new するので効率的 make_unique【C++14】 名前の一貫性、new の記述が不要になる 例 shared_ptr<T> sp = make_shared<T>(); unique_ptr<T> up = make_unique<T>(); unique_ptr はポインタをコピーし</t></t></t></t>…

【C++】ヌルポインタ

NULL【C++98】 #define NULL 0 と定義される、単なる数値の0 nullptr【C++11】 あらゆるポインタ型のヌルポインタを表すポインタ定数のキーワード 整数には変換できない 例 void f(int n); void f(const char *s); f(0); // f(int) f("hello"); // f(const …

【C++】ラムダ式

C++ における関数オブジェクト struct PlusOne { auto operator() (int x) const -> int { return x + 1; } }; PlusOne plusOne = {}; std::cout << plusOne(0) << std::endl; // 1 ラムダ式の値は、匿名の関数オブジェクトである auto lambda_0 = [](int x)…

【C++】 C++ の新しい typedef 方法

Before typedef int type1; typedef int type2[10]; typedef type1 (*type3)(type2); After using type1 = int; using type2 = int[10]; using type3 = type1 (*)(type2); 引用:本の虫: C++の新しいtypedef方法、alias declaration