boost库bind的一些用法

std::for_each(vect.begin(), vect.end(), std::mem_fun_ref(&class::fun));
std::for_each(vect.begin(), vect.end(), boost::bind(&class::fun, _1));
ptr
std::for_each(vect.begin(), vect.end(), std::mem_fun(&class::fun))
std::for_each(vect.begin(), vect.end(), boost::bind(&class::fun, _1));
std::string surname() const {
 return name_;
}
#include <iostream>
#include <boost/function.hpp>
bool some_func(int i, double d)
{
 return i > d;
}
int main()
{
 boost::function<bool (int, double)> f;
 f = &some_func;
 f(10, 1.1);
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/function.hpp>
void print_new_value(int i)
{
 std::cout << "The value has been updated and is now" << i << std::endl;
}
void interested_in_the_change(int i)
{
 std::cout << "Ah, the value has changed." << std::endl;
}
class notifier
{
 typedef void (*function_type)(int);
 std::vector<function_type> vec_;
 int value_;
public:
 void add_observer(function_type t)
 {
  vec_.push_back(t);
 }
 void change_value(int i)
 {
  value_ = i;
  for (std::size_t i=0; i<vec_.size(); ++i)
  {
   (*vec_[i]) (value_);
  }
 }
};
int main()
{
 notifier n;
 n.add_observer(&print_new_value);
 n.add_observer(&interested_in_the_change);
 n.change_value(42);
}


--------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/function.hpp>
class some_class
{
public:
 void do_stuff(int i) const
 {
  std::cout << "OK, Stuff is done. " << i << std::endl;
 }
};
int main()
{
 //boost::function<void(some_class, int)> f;
 //f=&some_class::do_stuff;
 //f(some_class(), 2);
 //boost::function<void(some_class&, int)> f;
 //f=&some_class::do_stuff;
 //some_class s;
 //f(s, 2);
 boost::function<void(some_class*, int)> f;
 f = &some_class::do_stuff;
 some_class s;
 f(&s, 3);
}
------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/function.hpp>
class keepint_state
{
 int total_;
public:
 keepint_state():total_(0) {}
 int operator()(int i)
 {
  total_ += i;
  return total_;
 }
 int total() const
 {
  return total_;
 }
};
int main()
{
 keepint_state ks;
 boost::function<int(int)> f1;
 //f1 = ks;
 f1 = boost::ref(ks);
 boost::function<int(int)> f2;
 //f2 = ks;
 f2 = boost::ref(ks);
 std::cout << "The current total is " << f1(10) << std::endl;
 std::cout << "The current total is " << f2(10) << std::endl;
 std::cout << "After adding 10 two times, the total is " << ks.total() << std::endl;
}


 data my(1, 11);
 if(std::binary_search(vec.begin(), vec.end(), my,
  boost::bind<bool>(
  std::less<unsigned int>(),
  boost::bind(&data::a_,_1),
  boost::bind(&data::a_,_2)
  )))
 {
 int a =0;
 }
 iter = std::lower_bound(vec.begin(), vec.end(), my,
  boost::bind<bool>(
  std::less<unsigned int>(),
  boost::bind(&data::a_,_1),
  boost::bind(&data::a_,_2)
  ));
 if(iter!=vec.end())
 {
  std::cout << "========" << std::endl;
  std::cout << (*iter).a_ << " " << (*iter).b_ << std::endl;
 }
 iter = std::upper_bound(vec.begin(), vec.end(), my,
  boost::bind<bool>(
  std::less<unsigned int>(),
  boost::bind(&data::a_,_1),
  boost::bind(&data::a_,_2)
  ));
 if(iter!=vec.end())
 {
  std::cout << "========" << std::endl;
  std::cout << (*iter).a_ << " " << (*iter).b_ << std::endl;
 }

  std::sort(
   vect_enter_copy_.begin(),
   vect_enter_copy_.end(),
   boost::bind<bool>(
   std::less<int>(),
   boost::bind(&enter_copy_data::id_, _1),
   boost::bind(&enter_copy_data::id_, _2)));

  //method 1
  //ptr_vect_type::iterator iter = std::find_if(
  // vect_enter_copy_.begin(),
  // vect_enter_copy_.end(),
  // boost::bind<bool>(
  // std::equal_to<int>(),
  // copy_id,
  // boost::bind(&enter_copy_data::get_id, _1)));
  //if(iter != vect_enter_copy_.end())
  // ret = (*iter).live_time_;
  //enter_copy_data data(copy_id);
  //method 2
  //ptr_vect_type::iterator iter = std::lower_bound(vect_enter_copy_.begin(), vect_enter_copy_.end(), data,
  // boost::bind<bool>(
  // std::less<unsigned int>(),
  // boost::bind(&enter_copy_data::id_, _1),
  // boost::bind(&enter_copy_data::id_, _2)
  // ));
  //if(iter!=vect_enter_copy_.end()) //{
  // ret = (*iter).live_time_;
  //}


#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/ptr_container/ptr_map.hpp>
struct my_class
{
 my_class()
 {
  std::cout << "my_class()" << std::endl;
 }
 ~my_class()
 {
  std::cout << "~my_class()" << std::endl;
 }
 my_class(const my_class& other)
 {
  a_ = other.a_;
  b_ = other.b_;
  std::cout << "(const my_class& other)" << std::endl;
  //return *this;
 }
 my_class & operator = (const my_class& rhs)
 {
  a_ = rhs.a_;
  b_ = rhs.b_;
  std::cout << "my_class & operator =" << std::endl;
  return *this;
 }
 int a_;
 int b_;
};
int main()
{
 boost::ptr_vector< my_class > vec;
 std::map<int, boost::ptr_vector< my_class > > mp;
 {
  my_class *my1 = new my_class();
  my1->a_ = 1;
  my1->b_ = 2;
  vec.push_back(my1);
  my_class *my2 = new my_class();
  my2->a_ = 11;
  my2->b_ = 22;
  vec.push_back(my2);
 }
 mp.insert(std::make_pair(1,vec));
 int index = 0;
 for(auto iter = mp.begin(); iter != mp.end(); ++iter)
 {
  for(auto i = (*iter).second.begin(); i != (*iter).second.end(); ++i, ++index)
   std::cout << "a_: " << (*iter).second.at(index).a_ << "b_: " << (*iter).second.at(index).b_ << std::endl;;
 }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/iterator/indirect_iterator.hpp>
#include <boost/bind.hpp>


using namespace std;


struct Element
{
int a;
int b;
};


class my_test
{
public:
my_test()
{


}

void ReferenceFn( Element & e );


void PointerFn( Element * e );


void Function();


void fn(int a, int b)
{
cout << a << b << endl;
}


void fn1()
{
vector< int > vec;
vec.push_back( 1 );
vec.push_back( 2 );
vec.push_back( 3 );


std::for_each(
vec.begin(), 
vec.end(), 
boost::bind(&my_test::fn, this, 5, _1)
);
}
};


void my_test::ReferenceFn( Element & e ) {  }


void my_test::PointerFn( Element * e ) { }


void my_test::Function()
{
std::vector< Element * > elements;
// add some elements...


// This works, as the argument is a pointer type
std::for_each( elements.begin(), elements.end(),
boost::bind( &my_test::PointerFn, boost::ref(*this), _1 ) );


// This fails (compiler error), as the argument is a reference type
//std::for_each( elements.begin(), elements.end(),
// boost::bind( &my_test::ReferenceFn, boost::ref(*this), _1 ) );


std::for_each( boost::make_indirect_iterator(elements.begin()), 
               boost::make_indirect_iterator(elements.end()),
               boost::bind( &my_test::ReferenceFn, boost::ref(*this), _1 ) );
}
int main()
{
my_test test;
test.fn1();


return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值