2012-02-01 (Wed)
■[Dart][JavaScript]Dart の JavaScript との対応表
Dart Synonym: Translations from JavaScript
JavaScript を知っている人なら役にたつかも。
2012-01-28 (Sat)
■[Music][Movie]【VOCALOIDオリジナル】終末26時【島白】
よだれPのcRockシリーズ26時。
【VOCALOIDオリジナル】終末26時【島白】 ‐ ニコニコ動画(原宿)
2012-01-15 (Sun)
■[Music][Movie]【ニコニコインディーズ】終末25時【島白】
島白(よだれP)さんの新曲を紹介。ギターが主役的な曲。
2012-01-05 (Thu)
■[C++]たまには adjacent_difference のことを思い出してあげてください
404 Blog Not Found:algorithm - mapBetween - 配列の隣接する2項にそれぞれ演算を施した配列
"各言語" に C++ は入っていないのか!と希望通り釣られてみる。
C++ なら標準装備されています。adjacent_difference を使いましょう。binary_op を指定すれば difference 以外も得られます。
#include<vector> #include<numeric> #include<iterator> #include<iostream> template<class Range> void print(Range const & r) { for(auto i: r) std::cout << i << ", "; std::cout << std::endl; } int main() { { std::vector<int> const in{0, 1, 4, 9}; std::vector<int> out; std::adjacent_difference(in.begin(), in.end(), std::back_inserter(out)); print(out); } { std::vector<int> const in{1, 2, 3, 4, 5}; std::vector<int> out; std::adjacent_difference(in.begin(), in.end(), std::back_inserter(out), [](int a, int b){return a + b;}); print(out); } }
実行結果:
0, 1, 3, 5, 1, 3, 5, 7, 9,
adjacent_difference は入力の要素数と同じ要素数だけ出力する。もし先頭要素がいらなければ range.begin() を捨てればいいだけのこと。
2011-12-23 (Fri)
■[C++][Boost]Boost.Algorithm を使ってみた (hex, unhex)
Boost.Algorithmが採択されました - Faith and Brave - C++で遊ぼう
Boost.Algorithmが採択されたそうです。早速一部使ってみました。
どうやらC++11で追加されたアルゴリズムのフォローがメインのようですが,見慣れないアルゴリズム(?)の hex と unhex を見つけたので使ってみました。ドキュメントによると MySQL の HEX, UNHEX がベースになっているそうですが,なんか微妙に違う感じです。
コード
#include<iostream> #include<vector> #include<iterator> #include<initializer_list> #include<cstdint> #include<boost/algorithm/hex.hpp> template<class Range> void p(Range const & r) { for(auto const & i: r) std::cout << i; std::cout << std::endl; } int main() { // hex std::cout << "hex sample\n"; // Iterator ver. { std::vector<long> const in{1, 2, 4, 8, 16, 32, 64, 128}; std::vector<char> out; boost::algorithm::hex(in.begin(), in.end(), std::back_inserter(out)); p(out); } // 0-terminated ver. { int const in[]{1, 2, 3, 4, 10, 14, 15, 16, 0}; char out[64]; boost::algorithm::hex(in, out); p(out); } // Range ver. { std::string out; boost::algorithm::hex(std::initializer_list<long long>{0xFLL, 0xFFLL, 0xFFFLL, 0xFFFFLL}, std::back_inserter(out)); p(out); } // unhex std::cout << "unhex sample\n"; // Iterator ver. { std::string const in{"00000000FFFFFFFF"}; std::vector<int32_t> out; boost::algorithm::unhex(in.begin(), in.end(), std::back_inserter(out)); p(out); } // 0-terminated ver. { char const in[] = "00C0FFEE"; int32_t out[1]; boost::algorithm::unhex(in, out); p(out); } // Range ver. { std::vector<uint32_t> out; out.resize(1); boost::algorithm::unhex(std::initializer_list<char>{'D', 'e', 'a', 'd', 'B', 'e', 'e','f'}, out.begin()); p(out); } }
実行結果
hex sample 0000000100000002000000040000000800000010000000200000004000000080 000000010000000200000003000000040000000A0000000E0000000F00000010 000000000000000F00000000000000FF0000000000000FFF000000000000FFFF unhex sample 0-1 12648430 3735928559

