Boost

First Post:

Last Update:

Word Count:
582

Read Time:
3 min

Boost库

获取版本

1
2
3
4
5
6
7
8
9
10
11
12
#include <boost/version.hpp>
#include <boost/config.hpp>
#include <iostream>
using namespace std;
int main() {
cout << BOOST_VERSION << endl;
cout << BOOST_LIB_VERSION << endl;
cout << BOOST_PLATFORM << endl;
cout << BOOST_COMPILER << endl;
cout << BOOST_STDLIB << endl;
return 0;
}

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <vector>
#include <iterator>
#include <algorithm>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost;
int main() {
boost::timer t;
boost::progress_display pd(100);
for (int i = 0; i < 100; ++i) {//进度条
usleep(1000 * 1000);
++pd;
}
boost::gregorian::date dt(2009, 12, 8); //date_time 库
assert(dt.year() == 2009);
assert(dt.day() == 8);
boost::gregorian::date::ymd_type ymd = dt.year_month_day();
std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is "
<<dt.day_of_year() <<" days of this year"<< std::endl;
std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //转换为其他格式
std::cout << boost::gregorian::to_iso_string(dt) << std::endl;
std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl;
//对数组排序操作
std::vector<int> test_vc(100);
std::vector<int>::iterator beg_it = test_vc.begin();
std::vector<int>::iterator end_it = test_vc.end();
std::srand(std::time(NULL));
std::for_each(beg_it, end_it, [](int& n){n = rand(); });
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl << std::endl;
std::sort(beg_it, end_it, std::greater<int>());
std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl<<std::endl;
boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6));

std::cout << t.elapsed() << "s" << std::endl; //程序运行时间

system("pause");
return 0;
}

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <boost/python.hpp>
#include <iostream>

struct StructionData {
void hello() {
std::cout << "hello, this is boost::python sample!\n";
}
void msg() {
std::cout << "print msg!\n";
}
};

BOOST_PYTHON_MODULE(Boost_Python_Sqmple) { //为静态编译
boost::python::class<StructionData>("StructionData")
.def("hello", &StructionData::hello)
.def("msg", &StructionData::msg);
}

Thread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/timer.hpp>
// g++ p3.cpp -lboost_thread -lpthread
using namespace std;
using namespace boost;

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

int main() {
boost::timer t;
boost::thread thr(&hello);
thr.join();

cout << t.elapsed_max() / 3600 << endl; //最大时间,单位:小时
cout << t.elapsed_min() << endl; //最小统计时间,单位: 秒
cout << t.elapsed() << endl; // 从建立对象开始,时间流失统计
}
打赏点小钱
支付宝 | Alipay
微信 | WeChat