c++
c++ で std::string と std::wstring (ワイド文字列版) を相互に変換するコードですプロトタイプ宣言 #include <string> std::wstring s2ws(const std::string& str); std::string ws2s(const std::wstring& wstr); 実装 #include <string> #include <codecvt> std::wstring s2ws(cons</codecvt></string></string>…
C++ のSFINAE (Substitution Failure Is Not An Error)を使って,クラス T に foo(int a, int b)があるか否かを調べる方法です. 簡単な例(class Tに オーバーロードされた関数がない場合) まずはシンプルに,クラス T に対してT::foo が存在するかどうか調…
一般化逆行列(擬似逆行列,最小二乗法に対応するやつ)をc++で計算するサンプル特異値分解(SVD)が必要なので,実装は Eigen を使いますpython/numpy の実装も用意しました.こちらです https://pyopyopyo.hatenablog.com/entry/2021/09/14/161955 #include <Eigen/Dense></eigen/dense>…
/dev/null みたいな std::ostream を作る方法. 方法1 : std::ostreamを継承して自前の null stream を作成する 以下のURLで紹介されている方法 stackoverflow.com #include <iostream> class NulStreambuf : public std::streambuf { char dummyBuffer[ 64 ]; protect</iostream>…
C言語やC++の欠点の一つは,バッファオーバーフローやオーバーランが起こりやすいことです.それは欠点であると同時にC/C++の利点であると言う人もいます.つまり,メモリ関連のチェックを省略することで,省略した分C/C++はオーバヘッドが少ない高速なプロ…
C++11を使うと綺麗に実装できます まず format() というテンプレート関数を作ります.C++11で新しく導入された,可変引数テンプレート,および std::snprintf() を使います. #include <string> #include <cstdio> #include <vector> template <typename ... Args> std::string format(const std::string</typename></vector></cstdio></string>…
今時のc++ (c++11など)でコピー禁止,代入禁止のクラスを作る方法 template <class T> struct CRTPnoncopyable { CRTPnoncopyable(const CRTPnoncopyable&) = delete; CRTPnoncopyable& operator=(const CRTPnoncopyable &) = delete; }; class A : CRTPnoncopyable<A> </a></class>…
c++で、ポインタからクラス名や型名を調べる処理は、以下の2ステップで実現できます typeid(obj).name() で型名の文字列を取る abi::__cxa_demangle() で demangle する サンプルコードは以下の通り #include <typeinfo> #include <cxxabi.h> #include <iostream> int main() { class MyCl</iostream></cxxabi.h></typeinfo>…
boost の boost/progress.hpp を使えばOK #include <boost/progress.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/thread/thread.hpp> int main(int argc,char *argv[]) { const unsigned long expected_count=100; boost::progress_display show_progress( expected_count ); for(int i=0; i</boost/thread/thread.hpp></boost/date_time/posix_time/posix_time.hpp></boost/progress.hpp>
boost-1.50.0 へ乗り換えました.移行方法をメモします. 1.50.0 での変更箇所 http://www.boost.org/users/history/version_1_50_0.html に一覧がありますが,機能追加・バグ修正・仕様変更と色々変わっています.特に大きな変更点としては,boost::filesys…