1、linux下载编译boost源码:
./bootstrap.sh
sudo
2、测试实例:
#include <cstdlib>
#include <iostream>
#include <string>
#include <boost/progress.hpp>
#include <boost/asio.hpp>
#include <fstream>
#include <vector>
using namespace std;
using namespace boost;
void test001() {
std::vector<std::string> v(100);
std::ofstream fs;
progress_display pd(v.size());
std::vector<string>::iterator it;
for (it = v.begin(); it != v.end(); ++it) {
fs << *it << std::endl;
++it;
cout << "*";
//sleep(1);
}
cout << endl;
}
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian_calendar.hpp>
void test002() {
boost::gregorian::date d(2010, 1, 30);
std::cout << d.year() << std::endl;
std::cout << d.month() << std::endl;
std::cout << d.day() << std::endl;
std::cout << d.day_of_week() << std::endl;
std::cout << d.end_of_month().day() << std::endl;
}
#include <boost/shared_ptr.hpp>
class Shared {
public:
Shared() {
std::cout << "ctor() called" << std::endl;
}
Shared(const Shared & other) {
std::cout << "copy ctor() called" << std::endl;
}
~Shared() {
std::cout << "dtor() called" << std::endl;
}
Shared & operator =(const Shared & other) {
std::cout << "operator = called" << std::endl;
}
};
void test003() {
typedef boost::shared_ptr<Shared> SharedSP;
typedef std::vector<SharedSP> VShared;
VShared v;
v.push_back(SharedSP(new Shared()));
v.push_back(SharedSP(new Shared()));
}
#include <boost/lexical_cast.hpp>
//字符串转换为整形
void test004(string str) {
int i = boost::lexical_cast<int>(str);
cout << i << endl;
}
#include <boost/format.hpp>
void test_format() {
// 使用%序号%的方式给出指示符, 后面用%连接对应的数据。
cout << boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50 << endl;
// 输出:writing toto, x=40.23 : 50-th try
// 可以延迟使用,顺序不必一致
boost::format fmter("%2% %1%");
fmter % 36;
fmter % 77;
cout << fmter << endl;
// 输出:77 36
// 可重用
fmter % 12;
fmter % 24;
cout << fmter << endl;
// 输出:24 12
// 可直接转成字符串
std::string s = fmter.str();
std::string s2 = str(boost::format("%2% %1% %2% %1%") % "World" % "Hello");
cout << s << endl << s2 << endl;
// 输出:
// 24 12
// Hello World Hello World
// 可以使用printf指示符
cout << boost::format("%3.1f - %.2f%%") % 10.0 % 12.5 << endl;
// 输出:10.0 - 12.50%
// printf指示符里使用N$指定使用第几个参数
cout << boost::format("%2$3.1f - %1$.2f%%") % 10.0 % 12.5 << endl;
// 输出:12.5 - 10.00%
}
#include <boost/algorithm/string.hpp>
void test005() {
string str("readme001readme002readme.txt");
if (ends_with(str, "txt")) {
cout << to_upper_copy(str) + " UPPER" << endl;
assert(ends_with(str, "txt"));
}
replace_first(str, "readme", "followme");
replace_all(str, "readme", "hehe");
cout << str << endl;
vector<char> v(str.begin(), str.end());
vector<char> v2 = to_upper_copy(erase_first_copy(v, "txt"));
for (int i = 0; i < v2.size(); ++i) {
cout << v2[i];
}
cout << endl;
}
#include <boost/tokenizer.hpp>
void test_tokenizer() {
string s("This is , a ,test!,中文,你好");
boost::tokenizer<> tok(s);
for (tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg) {
cout << *beg << "-";
}
cout << endl;
}
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/regex.hpp>
//需要编译器加上 -lboost_regex
//使用cppunit需要连接器加上 -lncurses -lcppunit -ldl
void test006() {
regex reg("a.c");
assert(regex_match("abc", reg));
//assert(regex_match("a + c", reg));
assert(!regex_match("ac", reg));
assert(!regex_match("abd", reg));
boost::regex reg2("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");
std::string correct = "123Hello N/A Hello";
std::string incorrect = "123Hello 12 hello";
assert(boost::regex_match(correct, reg2) == true);
assert(boost::regex_match(incorrect, reg2) == false);
}
#include <boost/any.hpp>
void test_any() {
typedef std::vector<boost::any> many;
many a;
a.push_back(2); //可以转换
a.push_back(string("test")); //转换失败
for (unsigned int i = 0; i < a.size(); ++i) {
cout << a[i].type().name() << "=";
try {
int result = any_cast<int>(a[i]);
cout << result << endl;
} catch (boost::bad_any_cast & ex) {
cout << "cast error:" << ex.what() << endl;
}
}
}
#include <boost/thread/thread.hpp>
void hello() {
std::cout << "Hello world, I'm a thread!" << std::endl;
}
//编译需要加上-lboost_system -L/usr/local/lib -lboost-thread
//-lboost_thread -lboost_system
//如果报错error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No ,
//则使用sudo ldconfig /home/shaochangqing/study/boost_1_49_0/stage/lib 或者export PATH=${PATH}:/usr/local/lib:/usr/local/include
int test_Thread() {
boost::thread thrd(&hello); // 译注:hello前面的&符号,可要可不要
thrd.join();
return 0;
}
//extern bool httpGet(/*out*/string& result, const string& host, uint16_t port, const string& url, boost::asio::io_service &_io);
/** 本程序基于boost1_55_0*/
int main(int argc, char** argv) {
test_Thread();
test001();
test002();
test003();
test004("123456");
test_format();
test005();
test_tokenizer();
test006();
test_any();
/**boost::asio::io_service io;
string str;
httpGet(str,"http://www.baidu.com/",80,"index.html",io);
cout<<str<<endl;*/
printf("\nok\n");
return 0;
}