10:18:18 # Life C++のalgorithm. map で remove_if を使おうとして挫折。 remove_if は mutable iterator を必要とするアルゴリズムなんだけど、mapはmutable iterator ではない。 map は順序を変更できないですよねぇ。 /usr/include/c++/4.3/bits/stl_algo.hを読んでみると、代入しているのでこれは使えない。 remove_copy_if なら使えるかなとおもったけどどうもこれもうまくいかなさそうな。 しかたがないので自分で実装してみる。
class MyPredicate {
public:
MyPredicate() : deleted_items_(0) {}
bool operator() (const pair<const int, string>& map_item) {
bool delete_this (map_item.second == "this" ||
map_item.second == "pen" ||
map_item.second == "fuga");
if (delete_this) {
deleted_items_ ++;
}
return delete_this;
}
string DebugString() {
stringstream ss;
ss << "items deleted: " << deleted_items_ << endl;
return ss.str();
}
private:
// I want to keep some statistics here for deleted items:
size_t deleted_items_;
};
template<class Map, typename Predicate>
void map_remove_if(Map& in_map, Predicate& should_delete) {
for (class Map::iterator i = in_map.begin();
i != in_map.end(); ) {
if (should_delete(*i)) {
in_map.erase(i++);
} else {
++i;
}
}
}
int main(int argc, char **argv) {
map<int, string> hoge;
hoge[1] = "test";
hoge[2] = "this";
hoge[5] = "is";
hoge[10] = "a";
hoge[11] = "pen";
hoge[12] = "fuga";
MyPredicate predicate;
map_remove_if(hoge, predicate);
cout << predicate.DebugString();
for (map<int,string>::iterator i = hoge.begin();
i != hoge.end(); i++) {
cout << i->first << ":" << i->second << endl;
}
}
11:34:33 # Life make の並列度。 make -l 3.0とか指定すると、load average の限界までジョブを開始してくれるらしい。
$Id: dancer-diary.el,v 1.94 2009/10/21 14:02:48 dancer Exp $