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
#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
#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;
}
#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;
}