ostringstreamを使う。
以下はファイル名の置き換えのために、幅を指定して`0'でpadding
するコード。
#include <iomanip>
#include <sstream>
int iconf = 300;
filename_base = "./config-$$$$$$.bin";
// `$$$$$$' of this file name should be replaced with iconf
// with padding of `0'.
string::size_type pos1 = filename_base.find_first_of("$");
string::size_type pos2 = filename_base.find_last_of("$");
size_t length = pos2 - pos1 + 1;
std::ostringstream ost;
ost << std::setw(length) << std::setfill('0') << iconf;
string num2 = ost.str();
string filename = filename_base;
filename.replace(pos1,length,num2);
// now, filename = "./config-000300.bin".
[14 Jan 2013]
引数の個数を指定せずに関数を呼出す形式。 受けた引数を扱うためのマクロを使う。
並列環境で printf をラップした関数をつくるのに使った。 [13 May 2011]
[19 Oct 2010]
Arguments を渡す constructor を持つクラスの配列を new
で生成したい場合。
(今苦労している問題。
constructor C++ array argument で検索)
[15 Oct 2010]
クラスの配列を作れるのは、引数なし constructor が用意されて いる場合だけのよう。 STLのコンテナなどでは、引数なし constructor でinstance を生成した後で、サイズの再設定が可能。 こういう手順を使うか、pointer を使うというのが解らしい。 [13 May 2011]