備忘録

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

2014-09-04から1日間の記事一覧

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