Boost.Spirit.PhoenixにRangeアルゴリズムがありました。
http://www.boost.org/doc/libs/1_42_0/libs/spirit/phoenix/doc/html/phoenix/algorithm.html
しかし、使ってみたら
for_each(v, [](int x) { std::cout << x << std::endl; });
と書いても何も起こらず。
findしてみたら
std::vector<int>::const_iterator it = find(v, 2);
型が違うと怒られ。
なんだこりゃと思って調べたら、こんな感じで使うようです。
#include <iostream> #include <vector> #include <list> #include <boost/spirit/home/phoenix/core/argument.hpp> #include <boost/spirit/home/phoenix/algorithm.hpp> using namespace boost::phoenix; using namespace boost::phoenix::arg_names; int main() { std::vector<int> v = {1, 2, 3}; // iteration algorithm for_each(_1, [](int x) { std::cout << x << std::endl; })(v); // querying algorithm std::vector<int>::const_iterator it = find(_1, 2)(v); std::cout << (it == v.end() ? "not found" : "found") << std::endl; // transformation algorithm transform(_1, v.begin(), [](int x) { return x * x; })(v); copy(_1, std::ostream_iterator<int>(std::cout, "\n"))(v); // list::sort std::list<int> ls = {3, 1, 4}; sort(_1)(ls); for_each(_1, [](int x) { std::cout << x << std::endl; })(ls); }
Rangeアルゴリズムの適用結果として関数オブジェクトactorが返ってくるので、
それに引数を渡して評価してあげないといけないみたいです。
それと、メンバのアルゴリズムが優先して呼ばれるような実装になっているため、
std::listに対してsortを適用することができるようです。
くわしい使い方はtestを見てください。
testは配列を使ってるのでわかりにくいところはわかりにくいですが。