boost::find_formatter
Boost C++ Libraries プログラミングを読みながらサンプルコード書いてみていたり.とりあえず頭からやろうと思って<boost/algorithm/string.hpp>を使った文字列処理であれこれ.find_formatterの使い方ではまったのでメモしておく.
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost; using namespace boost::algorithm; struct SimpleFormatter { template<typename FindResult> string operator()(const FindResult& match) { // FindResultの型は const FindResult& にするようだ // FindResultはイテレータを持つ何かの型になってるらしい return "[" + string(match.begin(), metch.end()) + "]"; } }; void main() { string s("Hello"); // head_finder は先頭2文字のRangeを取得(ここでは文字列か) // ***_copy系の関数を使えばストリームに直接繋げられる cout << find_format_copy(s, head_finder(s, 2), SimpleFormatter()) << endl; }
あ,そういえば,サンプルコードを打ち込むのにわざわざソースファイルを分けるのは面倒なので全部一つのファイルに書いていっていたらコンパイルがかなり遅くなったので,include文を問答無用で全部プリコンパイルヘッダーに移動してみたらだいぶ早くなった.サンプル書いていったらこれからどんどん増えそう.hppのファイルをこうするのも効果あるのかな?
// stdafx.h #include <iostream> #include <string> #include <algorithm> #include <vector> #include <boost/tokenizer.hpp> #include <boost/lexical_cast.hpp> #include <boost/format.hpp> #include <boost/algorithm/string.hpp>