2009-01-01から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>…

std::mem_fun_ref関数を自作する

C++

前回の記事でstd::mem_fun_ref関数というのを習いました。多分処理的には難しいことはしてないだとうと思い、何も見ずに自作してみました。 #include <iostream> #include <functional> #include <vector> #include <algorithm> using std::cout; using std::endl; template <class T> class CMyMemFunRef { pub</class></algorithm></vector></functional></iostream>…

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

C++

http://www.geocities.jp/ky_webid/cpp/library/025.html例えばvectorのようなクラス型を持つvectorだとして、std::for_each等でそのクラスのメンバ関数を呼びたい場合、通常のやり方ではできません。そこでメンバ関数アダプタという物を利用します。 #inclu…

関数アダプタ ネゲータ

C++

http://www.geocities.jp/ky_webid/cpp/library/025.html今回はネゲータについて勉強したいと思います。これも関数アダプタの一つです。要は叙述関数の反対の結果得るための物です。まず比較のためにネゲータを使わずに普通に叙述関数を使った処理を書いて見…

using namespace stdを利用しないようにする

C++

最近色々と覚えてきたので何がstdで何がstdじゃないのか混乱しそうになってきました。やはり多少面倒臭いのもありますが、今後の記事でusing namespace stdはなるべく使わないようにします。coutやendlなど、よく使う物に関しては別途usingで対応します。こ…

関数アダプタ バインダ

C++

http://www.geocities.jp/ky_webid/cpp/library/025.html関数アダプタとは他の関数オブジェクトや値を、関数オブジェクトに組み合わせて新しく作成する関数オブジェクトということらしい。これだけじゃ何のことかさっぱりわかりませんね。というわけで実際の…

標準の関数オブジェクト

C++

http://www.geocities.jp/ky_webid/cpp/library/024.htmlfunctionalヘッダというものに標準的な関数オブジェクトが定義されています。以下にそのリストを上げてみます。ちなみに関数オブジェクトの引数に渡される変数名をparam1とparam2だと仮定します。 関…

adjacent_difference関数

C++

http://www.geocities.jp/ky_webid/cpp/library/023.html各要素間の差を求めます。これも実装例を見たほうが早いですね。 #include <iostream> #include <numeric> #include <vector> using namespace std; int main () { vector<int> data1; vector<int> data2; data1.push_back(10); data1.push_</int></int></vector></numeric></iostream>…

partial_sum関数

C++

http://www.geocities.jp/ky_webid/cpp/library/023.html部分和を求めるための関数です。順番にある値と次の値の加算結果を格納していきます。実装を見たほうがはやいですね。 #include <iostream> #include <numeric> #include <vector> using namespace std; int main () { vector<int> dat</int></vector></numeric></iostream>…

inner_product関数

C++

http://www.geocities.jp/ky_webid/cpp/library/023.html各要素を*演算子で乗算し、その結果を+演算子で加算していきます。言葉だけではわかりにくいので、まずinner_productと同等の処理をfor文で書いてみます。 #include <iostream> #include <numeric> #include <vector> using names</vector></numeric></iostream>…

accumulate関数

C++

http://www.geocities.jp/ky_webid/cpp/library/023.html数値演算のアルゴリズムをいくつか勉強したいと思います。これらはalgorithmヘッダではなく、numericヘッダに定義されています。まずはaccumulate関数について勉強します。これは指定された範囲内のデ…

transform関数

C++

http://www.geocities.jp/ky_webid/cpp/library/022.htmlcopy関数に似ていますが、transform関数は関数(や関数オブジェクト)を渡して独自の処理をしながら要素のコピーを行えます。int型のvectorで試してみます。 #include <iostream> #include <algorithm> #include <vector> using names</vector></algorithm></iostream>…

stringstreamクラス

ロベールのC++入門講座 08-03文字列をcoutやcinのような扱い方と同じように扱えるようにしたのがstringstreamクラスです。使用例を見てみましょう。 #include <iostream> #include <sstream> using namespace std; int main () { stringstream sstr; sstr.str(""); sstr << "abc</sstream></iostream>…

stringクラスはヌルターミネーターを必要としない

ロベールのC++入門講座 08-01stringクラスはその文字列の長さを自前で管理しているため、ヌルターミネーターを使わなくても問題ない。それどころからヌルターミネーターを一つの文字列として認識することさえ可能である。 #include <iostream> #include <string> using namespa</string></iostream>…

仮想関数テーブル

ロベールのC++入門講座 07-09仮想関数テーブルという内部実装の話。ある仮想関数を呼ぼうとしたときに、実際にどの関数が呼ばれるのかという情報が書き込まれた表のこと。クラスの隠しメンバ変数にその情報を持っているのでクラスのサイズが増える。また仮想…

unique関数

C++

http://www.geocities.jp/ky_webid/cpp/library/021.html連続して重複する値が合った場合、削除してひとつにまとめます。使ってみます。 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main () { vector<int> data; data.push_back(3); data.push_back</int></algorithm></vector></iostream>…

remove関数

C++

http://www.geocities.jp/ky_webid/cpp/library/021.htmlremove関数で値の削除ができます。使ってみます。 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main () { vector<int> data; data.push_back(3); data.push_back(4); data.push_back(5); data</int></algorithm></vector></iostream>…

lower_bound関数とupper_bound関数

C++

http://www.geocities.jp/ky_webid/cpp/library/020.htmlソート済みの配列に値を挿入する場合、例えばpush_back等で追加した後にまたソートしなおさないといけないので冗長です。lower_boundとupper_boundを使えば、値の挿入位置を適切に検出できます。lower…

ファイルの読み書きfstreamクラス

ロベールのC++入門講座 08-04ファイルの読み書きはC言語のときに色々やりましたが、C++ではまだ殆どやってませんでした。当然Cのようにfopen等で同じような実装もできるのですが、折角なのでC++特有のファイルの読み書きの方法を勉強したいと思います。fstre…

binary_search関数

C++

http://www.geocities.jp/ky_webid/cpp/library/020.html既にソート済みのデータを対象に検索等を行うときに便利な関数の話です。まずはbinary_search関数を見てみます。 #include <iostream> #include <algorithm> #include <vector> #include <ctime> using namespace std; int main () { vector<int></int></ctime></vector></algorithm></iostream>…

partial_sortによる部分ソート

C++

http://www.geocities.jp/ky_webid/cpp/library/019.htmlpartial_sort関数を使えば、例えば先頭からn個までがソートされた時点で処理を中断するといったことができます。実例を見たほうが早いですね。 #include <iostream> #include <algorithm> #include <vector> using namespace std; </vector></algorithm></iostream>…