C++(BOOSTライブラリ)

インストール


手順
  • boostのインストールファイルをダウンロードする
  • インストールする
  • 「プロジェクト→プロパティ→構成プロパティ→C/C++→全般」と移動する
  • 「追加のインクルードディレクトリ」の欄に「C:\Program Files\boost\boost_1_51」(インストールしたディレクトリ)を入力

メルセンヌツイスター(擬似乱数)

サンプル1
  1. #include <iostream>
  2. #include <boost\random.hpp>
  3.  
  4. using namespace std;
  5. using namespace boost;
  6.  
  7. /* 1~9の整数 */
  8. mt19937 gen1( static_cast<unsigned long>( time(0) ));
  9. uniform_int<> dst1( 1, 9 );
  10. variate_generator< mt19937&, uniform_int<> > rand1( gen1, dst1 );
  11.  
  12. /* 0.0~5.0の実数 */
  13. mt19937 gen2( static_cast<unsigned long>( time(0) ));
  14. uniform_real<> dst2( 0.0, 5.0 );
  15. variate_generator< mt19937&, uniform_real<> > rand2( gen2, dst2 );
  16.  
  17. /* 0.0~1.0の実数 */
  18. mt19937 gen3( static_cast<unsigned long>( time(0) ));
  19. uniform_01<> dst3;
  20. variate_generator< mt19937&, uniform_01<> > rand3( gen3, dst3 );
  21.  
  22. int main(){
  23. for(int i=0;i<10;i++)cout << rand1() << endl;
  24. cout << endl;
  25.  
  26. for(int i=0;i<10;i++)cout << rand2() << endl;
  27. cout << endl;
  28.  
  29. for(int i=0;i<10;i++)cout << rand3() << endl;
  30. cout << endl;
  31.  
  32. return 0;
  33. }
  34.  

Format

表示の方式をprintfみたいにする
  1. #include <iostream>
  2. #include <boost/format.hpp>
  3.  
  4. using namespace std;
  5. using namespace boost;
  6.  
  7. int main(){
  8. string str = "num";
  9. int num = 10;
  10.  
  11. cout << boost::format("%1% = %2%") % str % num << endl;
  12. cout << boost::format("%2% = %1%") % str % num << endl;
  13.  
  14. cout << boost::format("[ %5d ]") % 123 << endl;
  15. cout << boost::format("[ %f ]") % 1.111 << endl;
  16. cout << boost::format("[ %.2f ]") % 1.111 << endl;
  17.  
  18. return 0;
  19. }
  20.  

出力
num = 10
10 = num
[   123 ]
[ 1.111000 ]
[ 1.11 ]


split・join

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <boost/algorithm/string.hpp>
  5.  
  6. using namespace std;
  7. using namespace boost;
  8.  
  9. int main(){
  10. string str = "AAA BBB2CCC DDD3EEE";
  11. vector<string> arr;
  12.  
  13. cout << "str : " << str << endl;
  14.  
  15. //文字列(str)を空白で区切ってVector(arr)に格納する
  16. boost::algorithm::split(arr, str, boost::algorithm::is_space() );
  17.  
  18. for(int i=0;i<(unsigned)arr.size();i++){
  19. cout << arr[i] << endl;
  20. }
  21.  
  22. //Vector(arr)の内容を指定文字列(===)で結合する
  23. str = boost::algorithm::join(arr, "===");
  24.  
  25. cout << "str : " << str << endl;
  26.  
  27. return 0;
  28. }
  29.  

timer

  1. #include <boost/timer.hpp>
  2. #include <iostream>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6. using namespace boost;
  7.  
  8. int main()
  9. {
  10. boost::timer tim; // タイマーの開始
  11.  
  12. //計測したい処理
  13.  
  14. cout << tim.elapsed() << " sec" << endl;
  15. return 0;
  16. }
  17.  
最終更新:2013年02月01日 01:50
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。