2009-08-25から1日間の記事一覧

バイナリモードでファイルの読み込み

ロベールのC++入門講座 08-09fstreamクラスを使ってのバイナリモードでファイルの読み込みをやってみます。 #include <iostream> #include <fstream> using std::cout; using std::endl; int main () { std::fstream file; char buf[16]; file.open("main.cpp",std::ios::in|std</fstream></iostream>…

バイトオーダー

ロベールのC++入門講座 08-08生データをバイナリモードで出力する場合、環境によってデータのバイトの並びが昇順になったり降順になったりするそうです。このバイトの並びのことをバイトオーダーと呼びます。そして昇順(左から右へ)の並びをビッグエンディア…

生データのファイル入出力

ロベールのC++入門講座 08-08C言語のときにもやりましたね。C++ではfstreamで同様のことが可能です。 #include <iostream> #include <fstream> #include <string> using std::cout; using std::endl; int main (int argc,const char* argv[]) { std::string type("w"); if ( argc >= 2 )</string></fstream></iostream>…

例外クラス

C++

http://www.geocities.jp/ky_webid/cpp/library/027.html標準で用意されている例外クラスの紹介がありました。どの例外クラスもstd::exceptionクラスから派生したものになっています。以下にその表を書いてみます。std::はスペースの都合上省きます。 except…

std::auto_ptrを自作する

C++

前回勉強したstd::auto_ptrですが、とても興味深い内容だったので自作してみました。myauto_ptrクラスです。 #include <iostream> #include <memory> using std::cout; using std::endl; template<class T> class myauto_ptr { public: myauto_ptr(T* p) { m_p = p; } myauto_ptr (const</class></memory></iostream>…

std::auto_ptr

C++

http://www.geocities.jp/ky_webid/cpp/library/026.htmlこれは以前、テンポラリバッファとしてのvector - (void*)Pないとの中で名前だけ出てきましたね。テンポラリバッファとしての単一オブジェクトをnewするときに便利といった感じなのでしょうか。見てみ…

std::ptr_fun関数を自作する

C++

関数ポインタアダプタであるstd::ptr_fun関数を自作してみました。 #include <iostream> #include <vector> #include <algorithm> using std::cout; using std::endl; template <class T> class CMyPtrFun { public: CMyPtrFun (const T func) { m_func = func; } template <class U> void operator()(const </class></class></algorithm></vector></iostream>…

関数ポインタアダプタ std::ptr_fun関数

C++

http://www.geocities.jp/ky_webid/cpp/library/025.htmlメンバ関数ポインタが使用可能なのなら関数ポインタだって使用可能です。ということで関数ポインタを渡せるようにしたのがstd::ptr_funという関数アダプタです。 #include <iostream> #include <functional> #include <vector> #incl</vector></functional></iostream>…

メンバ関数アダプタ std::mem_fun関数

C++

std::mem_fun_ref関数と殆ど同じです。vector等、コンテナに指定されているデータ型がポインタの場合に使うのがstd::mem_fun関数になります。 #include <iostream> #include <functional> #include <vector> #include <algorithm> using std::cout; using std::endl; class CSample { public: CSample(</algorithm></vector></functional></iostream>…

fstreamクラスでファイルのコピー処理

ロベールのC++入門講座 08-09ファイルのコピーを実装します。 #include <iostream> #include <fstream> using std::cout; using std::endl; int main () { std::fstream file1; std::fstream file2; char buf[16]; file1.open("main.cpp",std::ios::in|std::ios::binary); if ( </fstream></iostream>…