c++ 準標準庫學習(一) -- 持續更新
環境搭建---網上查詢
本人測試環境Ubuntu16.04
#include <iostream> #include <boost/version.hpp> #include <boost/config.hpp> using namespace std; int main(void) { cout << BOOST_VERSION << endl; cout << BOOST_LIB_VERSION << endl; cout << BOOST_PLATFORM << endl; cout << BOOST_COMPILER << endl; cout << BOOST_STDLIB << endl; return 0; } ~/Boost/test$a.out 106700 1_67 linux GNU C++ version 5.4.0 20160609 GNU libstdc++ version 20160609
noncopyable -- 不能複製的類(單例)
一、時間類
1.timer庫
標頭檔案: #include <boost/timer.hpp>
名字空間:using namespace boost;
timer類實現:
class timer{
public:
// 建構函式
timer()
{
_start_time = std:clock();
}
// 重新開始計時
void restart()
{
_start_time = std:clock();
}
// 已流逝的時間
// CLOCKS_PER_SEC -- macOS、linux下為1,000,000(精確微秒)
-- Windows下為1,000(精確毫秒)
double elapsed() const
{
return double(std:clock() - _start_time) / CLOCKS_PER_SEC;
}
// 測量的最小時間單位
double elapsed_min() const
{
return double(1) / double(CLOCKS_PER_SEC);
}
// 可測量的最大時間
// numeric_limits -- 標準庫中數值極限類
double elapsed_max() const
{
return (double((std:numeric_limits<std:: clock_t>::max)()) - double(_start_time) / double(CLOCKS_PER_SEC);
}
private:
std:clock_t _start_time; // 宣告成員變數_start_time;
};
timer類沒有解構函式
使用建議:
timer介面簡單,輕巧好用,適用於大部分要求不高的程式計時任務。
timer不適合高精度的時間測量任務,它的精度依賴於作業系統或編譯器,難以跨平臺。
timer也不適合大跨度時間段測量,如(天,月,年),推薦使用cpu_timer元件
#include <boost/timer.hpp> #include <iostream> using namespace boost; int main(void) { timer t; std::cout << "可度量最大時間:" << t.elapsed_max() / 3600 << "小時" << std::endl; std::cout << "可度量最小時間:" << t.elapsed_min() << "秒" << std::endl; std::cout << "已流逝時間:" << t.elapsed() << "秒" << std::endl; return 0; } ~/Boost/timer$a.out 可度量最大時間:2.56205e+09小時 可度量最小時間:1e-06秒 已流逝時間:0.000217秒
2.progress_timer類
progress_timer類派生於timer類,會在析構時自動輸出時間,省去手動呼叫elapsed()
標頭檔案: #include <boost/progress.hpp>
名字空間:using namespace boost;
class progress_timer:public timer, noncopyable
{
public:
explicit progress_timer();
progress_timer(std::ostream & os);
~progress_timer();
};
progress_timer類,在析構時允許輸出定向I/O流,預設是std::cout,可以用其他標準庫輸出流(ofstream,ostringstream)替換,或者cout.rdbuf()重定向cout的輸出。
8 #include <iostream>
9 #include <boost/progress.hpp>
10 using namespace boost;
11
12 int main(void){
13 progress_timer t;
14 int i = 8379016;
15 while (i > 0)
16 {
17 std::cout << i * i << std::endl;
18 i = i - 20;
19 }
20 return 0;
21 }
~/boost/a.out
55696
46656
38416
30976
24336
18496
13456
9216
5776
3136
1296
256
1.50 s
3.progress_display類
描述:該類可以在控制檯上顯示程式執行進度
標頭檔案:#include <booost/progress.hpp>
名字空間: using namespace boost;
class progress_diaplay:noncopyable
{
public:
progress_diaplay (unsigned long expect_count); // 建構函式
progress_diaplay (unsigned long expect_count,
std::ostream& os,
const std::string & s1 = "\n",
const std::string & s2 = "\n",
const std::string & s3 = "\n"); // 建構函式
void restart(unsigned long expect_count); // 重新開始計時
unsigned long operator += (unsigned long increment); // 累加進度(按一定值)
unsigned long operator ++(); // 按一累加
unsigned long count() const; // 返回當前的計數
unsigned long expect_count() const; //
};
progress_diaplay無法把進度顯示輸出與程式的輸出分離。因為他的輸出都指向標準輸出
#include <iostream>
#include <boost/progress.hpp>
#include <vector>
using namespace boost;
using namespace std;
int main(void){
vector <int> vec_num;
for (int i = 0; i < 50; ++i)
{
vec_num.push_back(i);
}
// 定義時間進度類
progress_display pd(vec_num.size());
for (int i = 0; i < vec_num.size(); ++i)
{
++pd;
}
return 0;
}
~/Boost/timer$a.out
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
---- 持續更新