网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
之前写了两篇关于在linux上安装boost和在Windows上使用boost的文章
下面就该开始使用boost了,boost包含的内容特别多,初学时我们肯定摸不着头脑,不知道从哪里开始学习比较好。
从我个人的看法,刚开始学习boost可以先找几个简单的模块用用,等熟悉了boost的一些基本概念和使用方法,后面就可以针对性的学习自己要用的模块。
二、boost基本概念
boost在安转完之后的目录结构如下图所示:
boost的模块都是以.hpp文件形式存在,即在一个文件中包括了函数变量的声明和定义,因此大部分boost模块我们可以直接包含使用,对于少部分模块我们需要编译成库文件使用。编译好的库文件存放在目录stage/lib里面
其余的.hpp文件都在boost目录下
因此我们要使用boost,最重要的就是boost目录和stage/lib目录,分别存放在 .hpp文件和 .lib文件。
在linux上使用即把boost目录加入到头文件查找目录,把stage/lib加入到库文件查找目录。
在Windows下,即在IDE软件中加入boost头文件查找目录,加入stage/lib到库文件查找目录,这个在之前的博客中已经讲了怎么操作,不知道的可以看看上面的博客。
三、日期与时间
1.timer
timer可以测量时间的流逝,是一个小型的计时器,可提供毫秒级别的计时精度和操作函数,供程序员手工控制使用,它就像个秒表。
直接看源代码,再来讲解
// boost timer.hpp header file ---------------------------------------------//
// Copyright Beman Dawes 1994-99. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE\_1\_0.txt or copy at http://www.boost.org/LICENSE\_1\_0.txt)
// See http://www.boost.org/libs/timer for documentation.
// Revision History
// 01 Apr 01 Modified to use new <boost/limits.hpp> header. (JMaddock)
// 12 Jan 01 Change to inline implementation to allow use without library
// builds. See docs for more rationale. (Beman Dawes)
// 25 Sep 99 elapsed\_max() and elapsed\_min() added (John Maddock)
// 16 Jul 99 Second beta
// 6 Jul 99 Initial boost version
#ifndef BOOST\_TIMER\_HPP
#define BOOST\_TIMER\_HPP
#include <boost/config/header\_deprecated.hpp>
BOOST\_HEADER\_DEPRECATED( "the facilities in <boost/timer/timer.hpp>" )
#include <boost/config.hpp>
#include <ctime>
#include <boost/limits.hpp>
# ifdef BOOST\_NO\_STDC\_NAMESPACE
namespace std { using ::clock_t; using ::clock; }
# endif
namespace boost {
// timer -------------------------------------------------------------------//
// A timer object measures elapsed time.
// It is recommended that implementations measure wall clock rather than CPU
// time since the intended use is performance measurement on systems where
// total elapsed time is more important than just process or CPU time.
// Warnings: The maximum measurable elapsed time may well be only 596.5+ hours
// due to implementation limitations. The accuracy of timings depends on the
// accuracy of timing information provided by the underlying platform, and
// this varies a great deal from platform to platform.
class timer
{
public:
timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
// timer( const timer& src ); // post: elapsed()==src.elapsed()
// ~timer(){}
// timer& operator=( const timer& src ); // post: elapsed()==src.elapsed()
void restart() { _start_time = std::clock(); } // post: elapsed()==0
double elapsed() const // return elapsed time in seconds
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
double elapsed\_max() const // return estimated maximum value for elapsed()
// Portability warning: elapsed\_max() may return too high a value on systems
// where std::clock\_t overflows or resets at surprising values.
{
return (double((std::numeric_limits<std::clock_t>::max)())
- double(_start_time)) / double(CLOCKS_PER_SEC);
}
double elapsed\_min() const // return minimum value for elapsed()
{ return double(1)/double(CLOCKS_PER_SEC); }
private:
std::clock_t _start_time;
}; // timer
} // namespace boost
#endif // BOOST\_TIMER\_HPP
- timer模块需要依赖下面三个文件
#include <boost/config.hpp>
#include <ctime>
#include <boost/limits.hpp>
- timer模块存在于命名空间namespace boost
- timer模块的主类为timer,包含1个构造函数,4个成员函数,1个成员变量
可以看出timer模块是非常简单的,当然我们刚开始学习一些简单的模块可以帮助我们建立一些使用boost的基础,让我们更快的上手,不失为一个好的选择。
下面我们用一点代码来简单使用一下timer模块
#include <iostream>
using namespace std;
#include <boost/timer.hpp>
using namespace boost;
int main()
{
timer t; //定义一个timer对象,通过该对象进行时间的相应操作
cout << CLOCKS_PER_SEC << endl;
cout << "max timespan:"
<< t.elapsed\_max() /3600 << "h" <<endl;
cout << "min timespan:"
<< t.elapsed\_min() << "s" << endl;
cout << "now time elapsed:"
<< t.elapsed() <<"s" << endl;
}
2.progress_timer
progress_timer也是一个计时器,它派生自timer,会在析构时自动输出时间,省去了timer手动调用elapsed()的工作,是一个相当方便的自动计时的小工具。
progress_timer位于命名空间boost,需要包含的头文件如下
#include <boost/progress.hpp>
using namespace boost;
先看progress_timer的源码
// boost progress.hpp header file ------------------------------------------//
// Copyright Beman Dawes 1994-99. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE\_1\_0.txt or copy at http://www.boost.org/LICENSE\_1\_0.txt)
// See http://www.boost.org/libs/timer for documentation.
// Revision History
// 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen)
// 20 May 01 Introduce several static\_casts<> to eliminate warning messages
// (Fixed by Beman, reported by Herve Bronnimann)
// 12 Jan 01 Change to inline implementation to allow use without library
// builds. See docs for more rationale. (Beman Dawes)
// 22 Jul 99 Name changed to .hpp
// 16 Jul 99 Second beta
// 6 Jul 99 Initial boost version
#ifndef BOOST\_PROGRESS\_HPP
#define BOOST\_PROGRESS\_HPP
#include <boost/config/header\_deprecated.hpp>
BOOST\_HEADER\_DEPRECATED( "the facilities in <boost/timer/timer.hpp> or <boost/timer/progress\_display.hpp>" )
#include <boost/timer.hpp>
#include <boost/noncopyable.hpp>
#include <boost/cstdint.hpp> // for uintmax\_t
#include <iostream> // for ostream, cout, etc
#include <string> // for string
namespace boost {
// progress\_timer ----------------------------------------------------------//
// A progress\_timer behaves like a timer except that the destructor displays
// an elapsed time message at an appropriate place in an appropriate form.
class progress\_timer : public timer, private noncopyable
{
public:
explicit progress\_timer( std::ostream & os = std::cout )
// os is hint; implementation may ignore, particularly in embedded systems
: timer(), noncopyable(), m\_os(os) {}
~progress\_timer()
{
// A) Throwing an exception from a destructor is a Bad Thing.
// B) The progress\_timer destructor does output which may throw.
// C) A progress\_timer is usually not critical to the application.
// Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
try
{
// use istream instead of ios\_base to workaround GNU problem (Greg Chicares)
std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
std::istream::floatfield );
std::streamsize old_prec = m_os.precision( 2 );
m_os << elapsed() << " s\n" // "s" is System International d'Unites std
<< std::endl;
m_os.flags( old_flags );
m_os.precision( old_prec );
}
catch (...) {} // eat any exceptions
} // ~progress\_timer
private:
std::ostream & m_os;
};
// progress\_display --------------------------------------------------------//
// progress\_display displays an appropriate indication of
// progress at an appropriate place in an appropriate form.
// NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax\_t, but
// found some compilers couldn't handle the required conversion to double.
// Reverted to unsigned long until the compilers catch up.
class progress\_display : private noncopyable
{
public:
explicit progress\_display( unsigned long expected_count_,
std::ostream & os = std::cout,
const std::string & s1 = "\n", //leading strings
const std::string & s2 = "",
const std::string & s3 = "" )
// os is hint; implementation may ignore, particularly in embedded systems
: noncopyable(), m\_os(os), m\_s1(s1), m\_s2(s2), m\_s3(s3) { restart(expected_count_); }
void restart( unsigned long expected_count_ )
// Effects: display appropriate scale
// Postconditions: count()==0, expected\_count()==expected\_count\_
{
_count = _next_tic_count = _tic = 0;
_expected_count = expected_count_;
m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
<< m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
<< std::endl // endl implies flush, which ensures display
<< m_s3;
if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
} // restart
unsigned long operator+=( unsigned long increment )
// Effects: Display appropriate progress tic if needed.
// Postconditions: count()== original count() + increment
// Returns: count().
{
if ( (_count += increment) >= _next_tic_count ) { display\_tic(); }
return _count;
}
unsigned long operator++() { return operator+=( 1 ); }
unsigned long count() const { return _count; }
unsigned long expected\_count() const { return _expected_count; }
private:
std::ostream & m_os; // may not be present in all imps
const std::string m_s1; // string is more general, safer than
const std::string m_s2; // const char \*, and efficiency or size are
const std::string m_s3; // not issues
unsigned long _count, _expected_count, _next_tic_count;
unsigned int _tic;
void display\_tic()
{
// use of floating point ensures that both large and small counts
// work correctly. static\_cast<>() is also used several places
// to suppress spurious compiler warnings.
unsigned int tics_needed = static\_cast<unsigned int>((static\_cast<double>(_count)
/ static\_cast<double>(_expected_count)) \* 50.0);
do { m_os << '\*' << std::flush; } while ( ++_tic < tics_needed );
_next_tic_count =
static\_cast<unsigned long>((_tic/50.0) \* static\_cast<double>(_expected_count));
if ( _count == _expected_count ) {
if ( _tic < 51 ) m_os << '\*';
m_os << std::endl;
}
} // display\_tic
};
} // namespace boost
#endif // BOOST\_PROGRESS\_HPP
3.data_time库
data_time库需要编译才能使用,data_time库包括两个部分,处理日期的gregorian,处理时间的posix_time,它们各自需要包含的头文件如下:
//处理日期的组件
#include <boost/date\_time/gregorian/gregorian.hpp>
using namespace boost::gregorian
//处理时间的组件
#include <boost/date\_time/posix\_time/posix\_time.hpp>
using namespace boost::posix_time
3.1gregorian
用下面一段代码来测试一下这个模块
#include <iostream>
using namespace std;
//#define DATE\_TIME\_NO\_DEFAULT\_CONSTRUCTOR
#include <boost/date\_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
//
void case1()
{
date d1;
date d2(2010,1,1);
date d3(2000, Jan , 1);
date d4(d2);
assert(d1 == date(not_a_date_time));
assert(d2 == d4);
assert(d3 < d4);
}
//
void case2()
{
date d1 = from\_string("1999-12-31");
date d2 ( from\_string("2015/1/1") );
date d3 = from\_undelimited\_string("20011118") ;
cout << d1 << d2 << d3 << endl;
cout << day_clock::local\_day() << endl;
cout << day_clock::universal\_day() << endl;
}
//
void case3()
{
date d1(neg_infin);
date d2(pos_infin);
date d3(not_a_date_time);
date d4(max_date_time);
date d5(min_date_time);
cout << d1 << d2 << d3 << d4 << d5 << endl;
try
{
//date d1(1399,12,1);
//date d2(10000,1,1);
date d3(2017,2,29);
}
catch(std::exception& e)
{
cout << e.what() << endl;
}
}
//
void case4()
{
date d(2017,6,1);
assert(d.year() == 2017);
assert(d.month() == 6);
assert(d.day() == 1);
date::ymd_type ymd = d.year\_month\_day();
assert(ymd.year == 2017);
assert(ymd.month == 6);
assert(ymd.day == 1);
cout << d.day\_of\_week() << endl;
cout << d.day\_of\_year() << endl;
assert(d.end\_of\_month() == date(2017,6,30));
cout << date(2015,1,10).week\_number() << endl;
cout << date(2016,1,10).week\_number() << endl;
cout << date(2017,1,10).week\_number() << endl;
assert(date(pos_infin).is\_infinity() );
assert(date(pos_infin).is\_pos\_infinity() );
assert(date(neg_infin).is\_neg\_infinity() );
assert(date(not_a_date_time).is\_not\_a\_date() );
assert(date(not_a_date_time).is\_special() );
assert(!date(2017,5,31).is\_special() );
}
//
void case5()
{
date d(2017,1,23);
cout << to\_simple\_string(d) << endl;
cout << to\_iso\_string(d) << endl;
cout << to\_iso\_extended\_string(d) << endl;
cout << d << endl;
//cout << "input date:";
//cin >>d;
//cout << d;
}
//
void case6()
{
date d(2017,5,20);
tm t = to\_tm(d);
assert(t.tm_hour == 0 && t.tm_min == 0);
assert(t.tm_year == 117 && t.tm_mday == 20);
date d2 = date\_from\_tm(t);
assert(d == d2);
}
//
void case7()
{
days dd1(10), dd2(-100), dd3(255);
assert( dd1 > dd2 && dd1 < dd3);
assert( dd1 + dd2 == days(-90));
assert((dd1 + dd3).days() == 265);
assert( dd3 / 5 == days(51));
weeks w(3);
assert(w.days() == 21);
months m(5);
years y(2);
months m2 = y + m;
assert(m2.number\_of\_months() == 29);
assert((y \* 2).number\_of\_years() == 4);
}
//
void case8()
{
date d1(2000,1,1),d2(2017,11,18);
cout << d2 - d1 << endl;
assert(d1 + (d2 - d1) == d2);
d1 += days(10);
assert(d1.day() == 11);
d1 += months(2);
assert(d1.month() == 3 && d1.day() == 11);
d1 -= weeks(1);
assert(d1.day() == 4);
d2 -= years(10);
assert(d2.year() == d1.year() + 7);
{
date d1(2017,1,1);
date d2 = d1 + days(pos_infin);
assert(d2.is\_pos\_infinity());
d2 = d1 + days(not_a_date_time);
assert(d2.is\_not\_a\_date());
d2 = date(neg_infin);
days dd = d1 - d2;
assert(dd.is\_special() && !dd.is\_negative());
}
{
date d(2017,3,30);
d -= months(1);
d -= months(1);
d += months(2);
assert(d.day() == 31);
}
}
//
void case9()
{
date_period dp1(date(2017,1,1), days(20));
date_period dp2(date(2017,1,1), date(2016,1,1));
date_period dp3(date(2017,3,1), days(-20));
date_period dp(date(2017,1,1), days(20));
assert(!dp.is\_null());
assert(dp.begin().day() == 1);
assert(dp.last().day() == 20);
assert(dp.end().day() == 21);
assert(dp.length().days() == 20);
{
date_period dp1(date(2017,1,1), days(20));
date_period dp2(date(2017,2,19), days(10));
cout << dp1; //[2010-Jan-01/2010-Jan-20]
assert(dp1 < dp2);
}
}
//
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
case9();
}
3.2date_period
data_time库使用date_period来表示日期区间的概念,它是时间轴上的一个左闭右开的区间,其端点是两个date对象。日期区间的左边界必须小于右边界,否则date_period将表示一个无效的日期区间。
date_period的类摘要如下:
class date\_period
{
public:
period(date, date);
period(date, days);
date begin() const;
date end() const;
date last() const;
days length() const;
bool is\_null() const;
bool operator==(const period &) const;
bool operator<(const period &) const;
void shift(const days &);
void expand(const days &);
bool contains(const date &) const;
bool contains(const period &) const;
bool intersects(const period &) const;
bool is\_adjacent(const period &) const;
bool is\_before(const date &) const;
bool is\_after(const date &) const;
period intersection(const date &) const;
period merge(const period &) const;
period span(const period &) const;
};
用一段代码来测试一下
#include <iostream>
using namespace std;
#include <boost/date\_time/gregorian/gregorian.hpp>
using namespace boost::gregorian;
//
void case1()
{
date_period dp(date(2017,1,1), days(20));
dp.shift(days(3));
assert(dp.begin().day() == 4);
assert(dp.length().days() == 20);
dp.expand(days(3));
assert(dp.begin().day() == 1);
assert(dp.length().days() == 26);
}
//
void case2()
{
date_period dp(date(2010,1,1), days(20));
assert(dp.is\_after(date(2009,12,1)));
assert(dp.is\_before(date(2010,2,1)));
assert(dp.contains(date(2010,1,10)));
date_period dp2(date(2010,1,5), days(10));
assert(dp.contains(dp2));
assert(dp.intersects(dp2));
assert(dp.intersection(dp2) == dp2);
date_period dp3(date(2010,1,21), days(5));
assert(!dp3.intersects(dp2));
assert(dp3.intersection(dp2).is\_null());
assert(dp.is\_adjacent(dp3));
assert(!dp.intersects(dp3));
}
//
void case3()
{
date_period dp1(date(2010,1,1), days(20));
**先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里**
**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/11782f7c6842f2a1c074dbce5e059dc5.png)
![img](https://img-blog.csdnimg.cn/img_convert/019a5d754b7973bfea69d818866b35b2.png)
![img](https://img-blog.csdnimg.cn/img_convert/6fbb5b835b06d39de626b0aed7589c93.png)
![img](https://img-blog.csdnimg.cn/img_convert/d584c0d3c009968482109b0dddd56187.png)
![img](https://img-blog.csdnimg.cn/img_convert/e8974c7fdf708c8e3a6edd4bb9223472.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**
assert(!dp.intersects(dp3));
}
//
void case3()
{
date_period dp1(date(2010,1,1), days(20));
**先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里**
**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
[外链图片转存中...(img-J2vqSEo9-1714843141462)]
[外链图片转存中...(img-tSfzIBfc-1714843141462)]
[外链图片转存中...(img-DZFV628Q-1714843141462)]
[外链图片转存中...(img-TRlMvixI-1714843141463)]
[外链图片转存中...(img-Mgr5e55m-1714843141463)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**