桁毎の処理に特化したコンテナ的なもの
を取り敢えず作っている。これからまだoperatorやその他機能(数値リテラルに対するアレコレなど)を付けるつもりだが一旦gitにコミットする。
使い方
このように桁毎に数値を分離してrangeにしたり、元の値に戻したり、range同士で加算が可能である。
#include<iostream> #include<experimental/iterator> #include<boost/type_index.hpp> #include<srook/math.hpp> int main() { int a=12345; srook::digits_values<> v(a); srook::digits_values<> test(std::move(a)); // move constructor for(std::size_t i=0; i<v.digit(); ++i)std::cout<<v[i]<<" "; // operator [] std::cout<<std::endl; srook::digits_values<> v1; v1=987654321; // assign ment (only value) for(auto&& i:v1)std::cout<<i<<" "; // iterator std::cout<<std::endl; auto value=v1.real_value(); // conversion to value std::cout<<value<<std::endl; auto v2=test+v1; // operator + std::copy(v2.cbegin(),v2.cend(),std::experimental::make_ostream_joiner(std::cout," ")); // iterator std::cout<<std::endl; srook::digits_values<int> w=12345; srook::digits_values<> x=98745; auto z=w+x; // w's result type is bigger than x. (x's result type is unsigne short int) std::cout<<boost::typeindex::type_id_with_cvr<decltype(z[0])>().pretty_name()<<std::endl; // so output is int. selecting bigger size type. }
結果。
1 2 3 4 5 9 8 7 6 5 4 3 2 1 987654321 10 10 10 10 10 4 3 2 1 int
作った理由としては、最近時間のあるときにpaizaの問題を解くのだがその中で桁毎に色々弄れるラッパーがあったら便利だな〜と思ったから。追い追い更新していく。