boost 编程

source: http://blog.sina.com.cn/s/blog_7e401538010141lx.html
  • boost::lexical_cast字符串向整型转换
#include <iostream>
#include<boost/lexical_cast.hpp>

using namespace std;
using namespace boot;
int main(){
    int a =lexical_cast<int>("1234");

    cout<<a <<endl;
}
//g++ test.cpp -o test
  • 最简单的调用方式(直接传递函数指针,没有参数和返回值)
#include <iostream>
#include<boost/thread/thread.hpp>

using namespace std;
using namespace boost;

void hello(){
    cout<< "hello"<< endl;
}

int main(){
    threadthrd(&hello);

    sleep(3);

    cout<< "end"<< endl;

    return 0;
}
//g++ test.cpp -lboost_thread -o test
  • 以对象的形式来构造thread(可以有参数)
#include <iostream>
#include<boost/thread/thread.hpp>

using namespace std;
using namespace boost;

struct suren{
    suren(int age):age(age){}

    void operator()(){
        cout << "operator: " << age<< endl;
    }

    int age;
};

int main(){
    threadthrd(suren(123));

    sleep(3);

    cout<< "end"<< endl;

    return 0;
}
//g++ test.cpp -lboost_thread -o test
  • 利用bind来传递参数
#include <iostream>
#include <boost/bind.hpp>
#include<boost/thread/thread.hpp>

using namespace std;
using namespace boost;

void hello(string name){
    if(name.empty()){
        name = "someone";
    }

    cout<< "hello "<< name<< endl;
}

int main(){
    threadthrd(bind(&hello, "mali"));

    sleep(3);

    cout<< "end"<< endl;

    return 0;
}
//g++ test.cpp -o test -lboost_thread
  • 线程睡眠

#include <iostream>

#include<boost/thread/thread.hpp>


using namespace std;

using namespace boost;


void test(){

    cout<< "hello sleep"<< endl;

}


int main(){

    threadthrd(&test);


   this_thread::sleep(posix_time::milliseconds(3000));


    return0;

}

//g++ test.cpp -o test -lboost_thread

  • 时间处理函数,获取当前时间,将时间类型转化为字符串,将字符串转化为时间类型,对于两个时间的比较可以直接使用逻辑判断符号来进行判断
#include <iostream>
#include <string>
#include<boost/date_time/posix_time/posix_time.hpp>

using namespace std;
using namespace boost;

int main(){
    string str_time =posix_time::to_iso_string(posix_time::second_clock::local_time());

    size_t pos  = str_time.length() - 2;

    str_time.replace(str_time.find("T"), 1, " ");
    str_time.replace(pos, 0,":");
    str_time.replace(pos -2, 0, ":");
    str_time.replace(pos -7, 0, ":");
    str_time.replace(pos -9, 0, ":");

    cout<< str_time<< endl;

    str_time.replace(str_time.find(" "), 1, "T");
    str_time.replace(str_time.find(":"), 1, "");
    str_time.replace(str_time.find(":"), 1, "");
    str_time.replace(str_time.find("-"), 1, "");
    str_time.replace(str_time.find("-"), 1, "");

    posix_time::ptime tm =posix_time::from_iso_string(str_time);

    cout<< tm<< endl;

    return 0;
}

//g++ test.cpp -o test
  • 获取某一个操作所用的时间

#include <iostream>

#inlucde<boost/date_time/posix_time/posix_time.hpp>


using namespace std;

using namespace boost;


int main(){

   posix_time::ptime time_from, time_to;

   posix_time::millisec_posix_time_system_config::time_duration_typetime_elapse;


    time_from= posix_time::microsec_clock::universal_time();


    for(int i= 0; i < 99999; ++i){

       //比较耗时的一些计算

    }


    time_to =posix_time::microsec_clock::universal_time();

   time_elapse = time_to - time_from;


    cout<< time_elapse.total_milliseconds()<< endl;


    return0;

}

  • 文件系统相关

#include <iostream>

#include <string>

#include<boost/filesystem.hpp>

 

int main(){
    usingnamespace std;

    usingnamespace boost;

 

    stringfile("1.txt");

 

   filesystem::path current_path = filesystem::current_path();

 

    cout<< "current_path : "<< current_path.string()<< endl;

 

   cout <<"is_directory : " <<filesystem::is_directory(current_path)<< endl;

 

    cout<< "file_size : "<<filesystem::file_size(filesystem::path(file))<< endl;

 

    return0;

}

  • 计算CRC校验值

#include <iostream>

#include <boost/crc.hpp>


int main(){

   boost::crc_32_type result;


   result.process_bytes("hello", 5);


    std::cout<< result.checksum()<< std::endl;


    return0;

}

  • 格式化输出

#include <iostream>

#include <string>

#include <boost/format.hpp>


int main(){

    strings;


    s =str(boost::format("x") % 12);


    std::cout<< s <<std::endl; //输出结果为0000000c


    return0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值