#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <limits>
#include <iostream>
#include <vector>
#include <boost/range/algorithm/for_each.hpp>
#include <boost/foreach.hpp>
#include <boost/ref.hpp>
struct minmax {
private:
int max_;
int min_;
public:
minmax() : min_(std::numeric_limits<int>::max()), max_(std::numeric_limits<int>::min()) {}
void operator() (int val) {
min_ = std::min( min_, val );
max_ = std::max( max_, val );
}
int get_min() const { return min_; }
int get_max() const { return max_; }
};
int main(int argc, const char* argv[]) {
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
{
minmax mm;
std::cout << "range foreach with functor" << std::endl;
boost::for_each( input, mm );
std::cout << mm.get_min() << "," << mm.get_max() << std::endl;
}
{
minmax mm;
std::cout << "range foreach with bind" << std::endl;
boost::for_each( input, boost::bind( &minmax::operator(), &mm, _1 ) );
std::cout << mm.get_min() << "," << mm.get_max() << std::endl;
}
{
minmax mm;
std::cout << "macro base foreach" << std::endl;
BOOST_FOREACH( int val, input ) {
mm( val );
}
std::cout << mm.get_min() << "," << mm.get_max() << std::endl;
}
return 0;
}
追記:これは、1,9 の集合を返す関数に変形すべき?
0 件のコメント:
コメントを投稿