-
/*
-
* bind.cpp
-
*
-
* Created on: 2013-7-16
-
* Author: root
-
*/
-
#include <vector>
-
#include <string>
-
#include <iostream>
-
#include <map>
-
-
#include <boost/bind.hpp>
-
-
using namespace std;
-
-
class personal_info
-
{
-
public:
-
std::string name_;
-
std::string surname_;
-
unsigned int age_;
-
-
public:
-
personal_info(const string& n,const string& s, unsigned int age):
-
name_(n),surname_(s),age_(age) { }
-
-
string name()const { return name_; }
-
-
string surname()const { return surname_; }
-
-
unsigned int age()const { return age_; }
-
};
-
//构造输出流
-
std::ostream& operator<<(std::ostream& os, const personal_info& pi)
-
{
-
os << pi.name_ << ' ' << pi.surname_ << ' ' << pi.age_ << '\n';
-
return os;
-
}
-
-
void print_string(const string& s)
-
{
-
cout << s << '\n';
-
}
-
-
//绑定的复制问题
-
class tracer
-
{
-
public:
-
tracer(){ cout << "tracer()\n"; }
-
tracer(const tracer& other) { cout << "tracer(const tracer& other)\n"; }
-
tracer& operator=(const tracer& other) { cout << "tracer& operator=(const tracer& other)\n"; return *this;}
-
~tracer(){ cout << "~tracer()\n"; }
-
-
void print(const string& s)
-
{
-
cout << s << '\n';
-
}
-
};
-
-
/*
-
int main()
-
{
-
//灵活对类结构体根据不成成员排序
-
std::vector<personal_info> vec;
-
-
vec.push_back(personal_info("Little","John",30));
-
vec.push_back(personal_info("Friar","Tuck",50));
-
vec.push_back(personal_info("Robben","Hord",40));
-
vec.push_back(personal_info("Jerry","Park",36));
-
vec.push_back(personal_info("Smith","Jim",33));
-
-
//按年龄排序
-
std::sort(vec.begin(),vec.end(),
-
boost::bind(
-
std::less<unsigned int>(),//第一个绑定函数,函数有括号,sort调用的less函数需要返回bool的值类型,但标准库函数都包含
-
// typedef result_type,就不需要显示给出值类型.完整为 boost::bind<bool>(std::less<unsigned int>(),_1,_2)
-
boost::bind(&personal_info::age,_1), //第二个为绑定器,函数无括号,_1为less的参数
-
boost::bind(&personal_info::age,_2)) );//第三个为绑定器,函数无括号,_2为less的参数
-
-
std::cout << vec[0] << '\n' <<
-
vec[1] << '\n' <<
-
vec[2] << '\n' <<
-
vec[3] << '\n' <<
-
vec[4] << '\n';
-
-
//按后缀名排序
-
std::sort(vec.begin(),vec.end(),
-
boost::bind(
-
std::less<std::string>(),//第一个绑定函数,函数有括号
-
boost::bind(&personal_info::surname,_1), //第二个为绑定器,函数无括号,_1为less的参数
-
boost::bind(&personal_info::surname,_2)) );//第三个为绑定器,函数无括号,_2为less的参数
-
-
std::cout << vec[0] << '\n' <<
-
vec[1] << '\n' <<
-
vec[2] << '\n' <<
-
vec[3] << '\n' <<
-
vec[4] << '\n';
-
//================================================
-
// 复制问题
-
tracer t;
-
boost::bind(&tracer::print,t,_1)(std::string("I'm call on a copy of it\n"));
-
//绑定时使用拷贝对象可以不依赖原对象的生命期,但会影响效率,可以使用使用boost::ref(t),或
-
boost::bind(&tracer::print,&t,_1)(std::string("I'm call on direct\n")); //此时bind依赖原对象的生命期
-
-
//================================================
-
//绑定到成员变量 直接获取得到data而不是key
-
std::map<int ,std::string> my_map;
-
my_map[0] = "smithy";
-
my_map[1] = "jerry";
-
-
// std::for_each(my_map.begin(),my_map.end(),
-
// boost::bind(&print_string,boost::bind(&std::map<int ,std::string>::value_type::second,_1)));
-
-
typedef std::map<int ,std::string> map_type;
-
std::for_each(my_map.begin(),my_map.end(),
-
boost::bind(&print_string,boost::bind(&map_type::value_type::second,_1)));
-
}
-
*/
阅读(1456) | 评论(0) | 转发(0) |