備忘録

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

【C++】

【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>…

【C++】クロージャ

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

【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

【C++】ゲーム開発者のための C++11/C++14 from Ryo Suzuki

引用:ゲーム開発者のための C++11/C++14 from Ryo Suzuki