boost::any库的使用

boost::any库的用法

最近尝试用boost库,作为另一代的C++标准,boost库确实很优秀。

boost::any可以存放任何类型的C++类型,也可以是用户自定义的类型。非常方便,可以很方便的满足在运行过程中判断数据类型,从而进行相关的操作。

函数原型:

// In header: <boost/any.hpp>


class any {
public:
  // construct/copy/destruct
  any();
  any(const any &);
  template<typename ValueType> any(const ValueType &);
  any & operator=(const any &);
  template<typename ValueType> any & operator=(const ValueType &);
  ~any();

  // modifiers
  any & swap(any &);

  // queries
  bool empty() const;
  const std::type_info & type() const;
};

成员函数说明:

any()

@param : NULL

@function:构造函数,构造一个空的any对象

any(const any&)

@param: const any&

@function:从另外一个常any对象构造一个any对象,类型一致

any(const ValueType &)

@param:ValueType&

@function:构造函数,类型可能不一致

operator = 

@param: const ValueType

@functiona:重载等号运算符

swap(any&)

@param :any&

@function:高效的交换两个any对象

empty()

@param:NULL

@function:判断一个any对象为不为空

type()

@param:NULL

@function:返回一个any对象含有的内部类型

例子:

/**
  2 @author Amiber
  3 @date 2012-12-13
  4 **/
  5 
  6 
  7 #include <iostream>
  8 #include <list>
  9 #include <typeinfo>
 10 #include <algorithm>
 11 
 12 
 13 #include <boost/any.hpp>
 14 
 15 
 16 typedef std::list<boost::any> many;
 17 
 18 void append_int(many& values,int value) 
 19 {
 20         boost::any to_append = value;
 21         values.push_back(to_append);
 22 }
 23 
 24 void append_string(many& values,const std::string& value)
 25 {
 26         values.push_back(value);
 27 }
 28 
 29 void append_char_ptr(many& values,const char* value)
 30 {
 31         values.push_back(value);
 32 }
 33 
 34 void append_any(many& values,const boost::any& value)
 35 {
 36         values.push_back(value);
 37 }
 38 
 39 void append_nothing(many& values)
 40 {
 41         values.push_back(boost::any());
 42 }
 43 
 44 
 45 bool is_empty(const boost::any& operand)
 46 {
 47         return operand.empty();
 48 }
 49 
 50 
 51 bool is_int(const boost::any& operand)
 52 {
 53         return operand.type() == typeid(int);
 54 }
 55 
 56 bool is_char_ptr(const boost::any& operand)
 57 {
 58         try
 59         {
 60                 boost::any_cast<const char*>(operand);
 61                 return true;
 62         }
 63         catch(const boost::bad_any_cast& badcast_e)
 64         {
 65                 return false;
 66         }
 67 }
 68 
 69 bool is_string(const boost::any& operand)
 70 {
 71         return boost::any_cast<std::string>(&operand);
 72 }
 73 
 74 
 75 void count_all(const many& values,std::ostream& out)
 76 {
 77         out<<"#empty=="<<std::count_if(values.begin(),values.end(),is_empty)<<std::endl;                                                                 
 78         out<<"#int == "<<std::count_if(values.begin(),values.end(),is_int)<<std::endl;
 79         out<<"#char* =="<<std::count_if(values.begin(),values.end(),is_char_ptr)<<std::endl;
 80         out<<"#string =="<<std::count_if(values.begin(),values.end(),is_string)<<std::endl;
 81 }
 82 
 83 
 84 int main(int argc,char* argv[])
 85 {
 86         many values;
 87         append_int(values,1);
 88         append_int(values,2);
 89         count_all(values,std::cout);
 90         return 0;
 91 }
                                                            


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值