boost总结之variant
来源:互联网 发布:长沙大火知乎 编辑:程序博客网 时间:2024/06/07 16:21
boost 是 C++“准”标准库(在低版本的标准库中没有,只能额外安装boost库,在较新的c++版本标准库已经加入了boost)。
下面主要学习boost库中variant的用法:
boost的variant库类似于联合体,但是联合体中只能接受POD类型,但variant中并无此限制,它可以接受任意的类型.
boost::variant <int, std::string , double> u;
u = 4;
int i = boost:: get<int >(u);
std::cout << "int : " << i << std ::endl;
u = "hello world!" ;
std::string s = boost::get <std:: string>(u );
std::cout << "string : " << s << std ::endl;
u = 1.2;
double d = boost:: get<double >(u);
std::cout << "double : " << d << std ::endl;
输出:
int : 4
string : hello world!
double : 1.2
string : hello world!
double : 1.2
要获取variant中的值时,一定要知道当前类型,并由get操作进行获取相应的值,如果转型失败,会抛出bad_get异常,get操作时运行时检测,我们可以通过检测get返回值来进行判断转型是否成功如下:
void printer (boost:: variant<int , std:: string, double >& u)
{
if (int *pi = boost::get <int>(& u))
{
std::cout << "int : " << * pi << std ::endl;
}
else if (std:: string *ps = boost:: get<std ::string>(& u))
{
std::cout << "string : " << * ps << std ::endl;
}
else if (double * pd = boost ::get< double>(&u ))
{
std::cout << "double : " << * pd << std ::endl;
}
}
通过printer函数中我们为get传进去的是variant的地址,返回如果是NULL的话则转型失败,进行下一个转型操作,依次来判断数据类型.可以看到通过get操作不仅需要知道需要转型的类型,而且还要在运行时判断转型是否成功,boost提供了编译时类型检测的安全访问方式,通过apply_visitor进行访问.
class printer_visitor : public boost::static_visitor <void>
{
public:
void operator ()(int i) const
{
std::cout << "int : " << i << std ::endl;
}
void operator ()(std:: string& s ) const
{
std::cout << "string : " << s << std ::endl;
}
void operator ()(double d) const
{
std::cout << "double : " << d << std ::endl;
}
};
int main ()
{
boost::variant <int, std::string , double> u;
printer_visitor visitor ;
u = 4;
boost::apply_visitor (visitor, u);
u = "hello world!" ;
boost::apply_visitor (visitor, u);
u = 1.2;
boost::apply_visitor (visitor, u);
}
我们定义一个variant时,如果我们没有给它赋任何值时,是不是他就是空呢?boost文档中"Never-Empty" Guarantee.也就是即使我们没有初始化,他也不会是空,默认的类型是我们模板参数的第一个,上例中默认的类型也就是int,值是0.
我们还可以将variant作为容器的元素例如:
std::vector <boost:: variant<int , std:: string, double > > v;
v.push_back (4);
v.push_back ("hello world!");
v.push_back (1.2);
std::for_each (v. begin(), v .end(), boost::apply_visitor (printer_visitor()));
阅读全文
1 0
- boost总结之variant
- Boost之Variant
- boost学习之-Variant
- boost之variant使用
- boost之variant
- Boost::Variant
- boost::variant
- C++学习 boost学习之-Variant
- C++boost库之variant使用
- C++boost库之variant使用
- Boost库之tuple、any和variant的使用
- BOOST中的union:boost::variant
- boost::variant and boost::apply_visitor
- boost::variant的简单介绍
- boost::any 与 boost::variant 的区别
- boost::any 与 boost::variant 的区别
- boost::tie()和boost::variant()讲解
- boost::tie()和boost::variant()讲解
- Oracle客户端(instantclient)以及plsql的安装与配置
- .NET 术语
- 关于阶乘的面试题总结
- 数组removeObjectIdenticalTo讲解
- 记一个腾讯信鸽引发的线程卡死问题
- boost总结之variant
- C++数组名a和&a的区别
- C++中的继承问题
- Windows下Java开发环境搭建(IDEA+JDK+Maven+Tomcat)
- R语言筛选出不重复的行的几种方法
- PAT 1038统计同成绩学生
- 系统服务的控制
- linux使用:配置SSH免密码登陆
- js控制Date类型转换为String类型