インストール
手順
- boostのインストールファイルをダウンロードする
- インストールする
- 「プロジェクト→プロパティ→構成プロパティ→C/C++→全般」と移動する
- 「追加のインクルードディレクトリ」の欄に「C:\Program Files\boost\boost_1_51」(インストールしたディレクトリ)を入力
メルセンヌツイスター(擬似乱数)
サンプル1
#include <iostream>
#include <boost\random.hpp>
using namespace std;
using namespace boost;
/* 1~9の整数 */
mt19937 gen1( static_cast<unsigned long>( time(0) ));
uniform_int<> dst1( 1, 9 );
variate_generator< mt19937&, uniform_int<> > rand1( gen1, dst1 );
/* 0.0~5.0の実数 */
mt19937 gen2( static_cast<unsigned long>( time(0) ));
uniform_real<> dst2( 0.0, 5.0 );
variate_generator< mt19937&, uniform_real<> > rand2( gen2, dst2 );
/* 0.0~1.0の実数 */
mt19937 gen3( static_cast<unsigned long>( time(0) ));
uniform_01<> dst3;
variate_generator< mt19937&, uniform_01<> > rand3( gen3, dst3 );
int main(){
for(int i=0;i<10;i++)cout << rand1() << endl;
cout << endl;
for(int i=0;i<10;i++)cout << rand2() << endl;
cout << endl;
for(int i=0;i<10;i++)cout << rand3() << endl;
cout << endl;
return 0;
}
Format
表示の方式をprintfみたいにする
#include <iostream>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
int main(){
string str = "num";
int num = 10;
cout << boost::format("%1% = %2%") % str % num << endl;
cout << boost::format("%2% = %1%") % str % num << endl;
cout << boost::format("[ %5d ]") % 123 << endl;
cout << boost::format("[ %f ]") % 1.111 << endl;
cout << boost::format("[ %.2f ]") % 1.111 << endl;
return 0;
}
出力
num = 10
10 = num
[ 123 ]
[ 1.111000 ]
[ 1.11 ]
split・join
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main(){
string str = "AAA BBB2CCC DDD3EEE";
vector<string> arr;
cout << "str : " << str << endl;
//文字列(str)を空白で区切ってVector(arr)に格納する
boost::algorithm::split(arr, str, boost::algorithm::is_space() );
for(int i=0;i<(unsigned)arr.size();i++){
cout << arr[i] << endl;
}
//Vector(arr)の内容を指定文字列(===)で結合する
str = boost::algorithm::join(arr, "===");
cout << "str : " << str << endl;
return 0;
}
timer
#include <boost/timer.hpp>
#include <iostream>
#include <windows.h>
using namespace std;
using namespace boost;
int main()
{
boost::timer tim; // タイマーの開始
//計測したい処理
cout << tim.elapsed() << " sec" << endl;
return 0;
}
最終更新:2013年02月01日 01:50