boost常用库

1.boost::any

boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内,最重要的它是类型安全的。有点象COM里面的variant

使用方法:

any::type() 返回包装的类型

any_cast可用于any到其他类型的转化

  1. #include <boost/any.hpp>
  2. void test_any()
  3. {
  4.     typedef std::vector<boost::any> many;
  5.     many a;
  6.     a.push_back(2);
  7.     a.push_back(string("test"));
  8.     for(unsigned int i=0;i<a.size();++i)
  9.     {
  10.         cout<<a[i].type().name()<<endl;
  11.         try
  12.         {
  13.             int result = any_cast<int>(a[i]);
  14.             cout<<result<<endl;
  15.         }
  16.         catch(boost::bad_any_cast & ex)
  17.         {
  18.             cout<<"cast error:"<<ex.what()<<endl;
  19.         }
  20.     }
  21. }

 

2.boost::array

boost::array仅仅是对数组一层薄薄的封装,提供跟各种算法配合的iterator,使用方法很简单。注意:可以使用{}来初始化array,因为array所有的成员变量都是public的。

  1. #include <boost/array.hpp>
  2. void test_array()
  3. {
  4.     array<int,10> ai = {1,2,3};
  5.     for(size_t i=0;i<ai.size();++i)
  6.     {
  7.         cout<<ai[i]<<endl;
  8.     }
  9. }

 

3.boost::lexical_cast

lexical_cast用于将字符串转换成各种数字类型(int,float,short etc.)

  1. #include <boost/lexical_cast.hpp>
  2. void test_lexical_cast()
  3. {
  4.     int i = boost::lexical_cast<int>("123");
  5.     cout << i << endl;
  6. }

 

4.boost::format

boost::format是用于替代c里面的sprintf,优点是类型安全,不会因为类型和参数不匹配而导致程序崩溃了,而且还可以重复使用参数。

  1. #include <boost/format.hpp>
  2. void test_format()
  3. {
  4.     cout << 
  5.         boost::format("writing %1%,  x=%2% : %3%-th try")
  6.         % "toto"
  7.         % 40.23
  8.         % 50
  9.         <<endl;
  10.     format f("a=%1%,b=%2%,c=%3%,a=%1%");
  11.     f % "string" % 2 % 10.0;
  12.     cout << f.str() << endl;
  13. }

 

5.boost::tokenizer

boost::tokenizer是用于切割字符串的,类似于Java里面的StringTokenizer

  1. #include <boost/tokenizer.hpp>
  2. void test_tokenizer()
  3. {
  4.     string s("This is  , a ,test!");
  5.     boost::tokenizer<> tok(s);
  6.     for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg)
  7.     {
  8.         cout << *beg << " ";
  9.     }
  10. }

 

6.boost::thread

boost::thread是为了提供跨平台的thread机制。利用boost::function来完成委托。

  1. #include <boost/thread.hpp>
  2. void mythread()
  3. {
  4.     cout<<"hello,thread!"<<endl;
  5. }
  6. void test_thread()
  7. {
  8.     boost::function< void () > f(mythread);
  9.     boost::thread t(f);
  10.     t.join();
  11.     cout<<"thread is over!"<<endl;
  12. }


7.boost::serialization

boost::serialization提供object的序列化功能。而且提供好几种序列化的格式,比如text,binary,xml

  1. #include <boost/archive/text_oarchive.hpp>
  2. #include <boost/archive/text_iarchive.hpp>
  3. #include <boost/archive/xml_oarchive.hpp>
  4. void test_serialization()
  5. {
  6.     boost::archive::text_oarchive to(cout , boost::archive::no_header);
  7.     int i = 10;
  8.     string s = "This is a test ";
  9.     to & i;
  10.     to & s;
  11.     ofstream f("test.xml");
  12.     boost::archive::xml_oarchive xo(f);
  13.     xo & BOOST_SERIALIZATION_NVP(i) & BOOST_SERIALIZATION_NVP(s);
  14.     boost::archive::text_iarchive ti(cin , boost::archive::no_header);
  15.     ti & i & s;
  16.     cout <<"i="<< i << endl;
  17.     cout <<"s="<< s << endl;
  18. }


8.boost::function

boost::function就是所谓的泛函数,能够对普通函数指针,成员函数指针,functor进行委托,达到迟调用的效果。

  1. #include <boost/function.hpp>
  2. int foo(int x,int y)
  3. {
  4.     cout<< "(foo invoking)x = "<<x << " y = "<< y <<endl;
  5.     return x+y;
  6. }
  7. struct test
  8. {
  9.     int foo(int x,int y)
  10.     {
  11.         cout<< "(test::foo invoking)x = "<<x << " y = "<< y <<endl;
  12.         return x+y;
  13.     }
  14. };
  15. void test_function()
  16. {
  17.     boost::function<int (int,int)> f;
  18.     f = foo;
  19.     cout << "f(2,3)="<<f(2,3)<<endl;
  20.     test x;
  21.     /*f = std::bind1st(std::mem_fun(&test::foo), &x);*/
  22.     boost::function<int (test*,int,int)> f2;
  23.     f2 = &test::foo;
  24.  
  25.     cout << "f2(5,3)="<<f2(&x,5,3)<<endl;
  26. }


9.boost::shared_ptr

boost::shared_ptr就是智能指针的实现,不象std::auto_ptr,它是可以stl的容器一起使用的,非常的方便。

  1. #include <boost/shared_ptr.hpp>
  2. class Shared
  3. {
  4.     public:
  5.         Shared()
  6.         {
  7.             cout << "ctor() called"<<endl;
  8.         }
  9.         Shared(const Shared & other)
  10.         {
  11.             cout << "copy ctor() called"<<endl;
  12.         }
  13.         ~Shared()
  14.         {
  15.             cout << "dtor() called"<<endl;
  16.         }
  17.         Shared & operator = (const Shared & other)
  18.         {
  19.             cout << "operator =  called"<<endl;
  20.         }
  21. };
  22. void test_shared_ptr()
  23. {
  24.     typedef boost::shared_ptr<Shared> SharedSP;
  25.     typedef vector<SharedSP> VShared;
  26.     VShared v;
  27.     v.push_back(SharedSP(new Shared()));
  28.     v.push_back(SharedSP(new Shared()));
  29. }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/509816.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HugeGraphServer 部署安装

官方文档链接&#xff1a;https://hugegraph.github.io/hugegraph-doc HugeGraphServer Quick Start 1 概述 HugeGraph-Server 是 HugeGraph 项目的核心部分&#xff0c;包含Core、Backend、API等子模块。 Core模块是Tinkerpop接口的实现&#xff0c;Backend模块用于管理数…

java中调用System.currentTimeMillis()获取当前时间来求出时间差及程序执行的时间

package com.test;import java.util.Scanner; //String的连接1000次 class StringOperate{private String str;public long doWork(){long beginSystem.currentTimeMillis();//开始时间setStr(null);for(int i0;i<10000;i){setStr(getStr() i);}long endSystem.currentTime…

HugeGraph 多图配置

多图配置 我们的系统是可以存在多个图的&#xff0c;并且各个图的后端可以不一样&#xff0c;比如图 hugegraph 和 hugegraph1&#xff0c;其中 hugegraph 以 cassandra 作为后端&#xff0c;hugegraph1 以 rocksdb作为后端。 配置方法也很简单&#xff1a; 修改 gremlin-se…

JanusGraph: 可视化 Gephi 插件安装

下载地址 https://gephi.org/ 安装 下一步默认安装即可 打开安装插件 打开后发现提示无法连接&#xff0c;并且可用插件显示为0 &#xff08;我更新过所以不为0&#xff09; 点击“代理配置” 设置代理 安装插件 Graph Streaming 继续 重新启动&#xff0c;安装完成 点击概…

【Boost】Boost使用几条简单笔记

头文件就是库 使用者最常问的问题就是“我该怎么安装Boost”&#xff0c;这个也是我一开始最关心的问题&#xff0c;Boost这点做的很好&#xff0c;将大部分实现都封装在头文件里&#xff0c;所以对于一些基本的Boost库&#xff0c;其实是不需要安装的&#xff0c;只需要将头文…

分析模板方法设计模式

首先来看看用来比较int和String分别进行1000次运算求需要的时间进行比较&#xff0c;代码的实现&#xff1a; package com.test;import java.util.Scanner; //String的连接1000次 class StringOperate{private String str;public long doWork(){long beginSystem.currentTimeM…

JanusGraph(HugeGraph通用): 可视化 GraphEXP 插件安装

JanusGraph&#xff1a; 可视化 Gephi 插件安装可参考&#xff1a;https://datamining.blog.csdn.net/article/details/103894994 下载地址&#xff1a;https://github.com/bricaud/graphexp 安装 解压&#xff0c;在根目录下修改 graphexp.html <div class"nav inp…

java中的接口的定义以及实现关系

一、什么是接口&#xff1f; - 硬件接口&#xff1a;设备之间的连接方式 - 软件接口&#xff1a;程序代码&#xff0c;特殊的类&#xff0c;表示一种规范&#xff0c;是具有N个方法的特征集合。 java中的接口&#xff1a; 专家说的多个抽象类的抽象就是接口。 interface …

迁移 Cloudera Manager 节点 ,迁移Cloudera Scm Server端

目录 1.迁移节点信息&#xff1a;212 -> 44 2.压缩存储目录&#xff0c;备份SCM Server数据 3.在新服务器安装server端 4.修改 cloudera-scm-server 配置文件 5.解压恢复备份数据到新服务器指定配置的目录中 6.修改所有agent节点的配置文件内容 7.迁移ClouderaManage…

CM,CDH 修改迁移元数据库

目录 1.停止所有服务 2.停止Cloudera Management Service服务 3.登录CM所在服务器&#xff0c;停止cloudera-scm-server服务&#xff0c;命令如下&#xff1a; 4.停止CM服务和CDH集群后&#xff0c;对数据库进行迁移 5.登录cloudera-scm-server服务所在服务器&#xff0c;…

面向接口编程思想

package com.test; /*面向接口编程&#xff1a;多态的好处&#xff1a;把实现类对象赋给接口类型变量&#xff0c;屏蔽了不同实现类之间的差异&#xff0c;从而可以做到通用编程 案例&#xff1a;使用USB设备来工作。*/ //指定USB规范 interface IUSB{void swapData(); } class…

Cloudera-Manager-agent 误删恢复

场景&#xff1a; 同事操作失误&#xff0c;将agent节点误删了 解决方法 1.对比正常agent节点与被删除节点安装包差异 yum list installed |grep cloudera 2.通过和正常的服务器对比我们发现&#xff0c;丢失的只有cloudera-manager-agent.x86_64 3.查看yum源中所有clouder…

java中的内部类

内部类&#xff1a;定义在类结构中的另一个类&#xff1a; 类中的定义的成员&#xff1a;字段、方法、内部类 为什么使用内部类&#xff1f; 增强封装&#xff0c;把内部类隐藏在外部类之内&#xff0c;不许其他类访问该类。内部类能提高代码的可读性和可维护性&#xff0c;…

JanusGraph 安装

下载地址&#xff1a;https://github.com/JanusGraph/janusgraph/releases/ 版本&#xff1a;Version 0.3.2 (June 16, 2019) 安装 解压 janusgraph-0.3.2-hadoop2.zip 文件 janusgraph单机版安装 注&#xff1a;本次安装janusgraph基于es和hbse&#xff0c;所以先安装es和…

java中枚举类型详解

枚举类型的引入&#xff1a; 枚举是从java5开始提供的一种新的数据类型&#xff0c;是一个特殊的类&#xff0c;就是固定的多个常量对象的集合。 定义格式&#xff1a; [修饰]enum 枚举类名 { 常量A,常量B,常量C; } 代码&#xff1a; package com.test;enum Weekday {…

Hbase 预写日志WAL处理源码分析之 LogCleaner

目录 Hlog WALs和oldWALs 整体流程 HMaster 初始化 定时执行 LogCleaner 日志清理类 ReplicationLogCleaner 日志清理类 总结 Hlog WALs和oldWALs 这里先介绍一下Hlog失效和Hlog删除的规则 HLog失效&#xff1a;写入数据一旦从MemStore中刷新到磁盘&#xff0c;…

开发者都应该使用的10个C++11特性

摘要 在C11新标准中&#xff0c;语言本身和标准库都增加了很多新内容&#xff0c;本文只涉及了一些皮毛。不过我相信这些新特性当中有一些&#xff0c;应该成为所有C开发者的常规装备。你也许看到过许多类似介绍各种C11特性的文章。下面是我总结的&#xff0c;C开发者都需要学习…

Java8新特性:CompletableFuture 方法介绍

目录 1. runAsync 和 supplyAsync方法 2. whenComplete、whenCompleteAsync、exceptionally 3. thenApply 、 handle thenApply handle 4.thenAccept 、thenRun 方法 消费处理结果 thenAccept thenRun 5. thenCombine 、 thenAcceptBoth thenCombine thenAcceptBot…

Java 8 CompletableFuture 教程

Java 8 有大量的新特性和增强如 Lambda 表达式&#xff0c;Streams&#xff0c;CompletableFuture等。在本篇文章中我将详细解释清楚CompletableFuture以及它所有方法的使用。 什么是CompletableFuture&#xff1f; 在Java中CompletableFuture用于异步编程&#xff0c;异步编…