Boost

Introduction

C++の標準ライブラリの強化。 STLもそうだが、うまく使えば幸せになれる。 ただし、変態的なコードも生成可能。

参考

install

tar zxvf boost_1_35_0.tar.gz
mv boost_1_35_0 boost_1_35_0_00
cd boost_1_35_0_00
mkdir -p /home/usrname/opt/pkgs/boost_1_35_0
./configure --prefix=/home/usrname/opt/pkgs/boost_1_35_0
make
make check
make install
ln -s /home/usrname/opt/pkgs/boost_1_35_0 /home/usrname/opt/boost
boostのcheckにはかなりの時間がかかる。

install [Boost Sandbox]

Boost Sandbox
The Boost Sandbox (housed in the sandbox subdirectory of the Subversion repository) contains unreviewed code that is intended to eventually become a part of the Boost libraries.
svn co http://svn.boost.org/svn/boost/sandbox boost-sandbox

install [Boost Numeric Bindings]

Under construction!

http://mathema.tician.de/software/boost-bindings
tar xvfz /where/to/you/downloaded/it/boost-numeric-bindings-NNNNNNNN-.tar.gz
cd boost-numeric-bindings
./configure --prefix=PATH
make install

ls PATH/include/boost-numeric-bindings/boost/numeric/bindings
amos  atlas  blas  lapack  mumps  traits  umfpack
ln -sf PATH/include/boost-numeric-bindings/boost/numeric/bindings BOOST_PATH/include/boost-1_34_1/boost/numeric/
ls BOOST_PATH/include/boost-1_34_1/boost/numeric/
bindings  conversion  interval  interval.hpp  ublas

Tips

文字列

regex

正規表現 in boost。

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main(int argc,char **argv)
{
  boost::regex regex("(\\.)|( )|(,)") ;
  const std::string s = "hoge.aho hige,baka" ;

  std::cout << s << std::endl ;
  std::cout << boost::regex_replace( s, regex, "_", boost::format_all) << std::endl ;

  return 0 ;
}

g++ -o test_boost_regex.o -c test_boost_regex.cc -I. -I/path/boost/include -Wall
g++ -o test_boost_regex test_boost_regex.o -L/path/boost/lib/ -lboost_regex
./test_boost_regex 
hoge.aho hige,baka
hoge_aho_hige_baka

lexical_cast

何でも変換。

#include <string>
#include <boost/lexical_cast.hpp>
using namespace std ;

int main(int argc,char **argv)
{
  uint a = 23 ;
  double x = 1.73205 ;

  string A = boost::lexical_cast<string>(a) ;
  string X = boost::lexical_cast<string>(x) ;
  printf( "a=%s, x=%s\n", A.c_str(), X.c_str()) ;

  int aa = boost::lexical_cast<int>(A) ;
  double xx = boost::lexical_cast<double>(X) ;
  printf( "a=%d, x=%lf\n", aa, xx) ;

  return 0 ;
}

split

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std ;

int main(int argc,char **argv)
{
  string field = "q19_taua.demod_gain" ;
  vector<string> split ;

  boost::algorithm::split( split, field, boost::is_any_of(".")) ;

  for( uint i=0; i<split.size(); i++){
    cout << split[i] << endl ;
  }

  return 0 ;
}

trim

左右の空白を除去。

#include <sys/types.h>
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>

int main(int argc,char **argv)
{
  std::string s = "   123  456 789 " ;
  std::cout << s << std::endl ;

  boost::trim(s) ;
  std::vector<std::string> v_s ;
  boost::split( v_s, s, boost::is_space(), boost::algorithm::token_compress_on) ;
  for( uint i=0; i<v_s.size(); i++){
    std::cout << v_s[i] << std::endl ;
  }

  return 0 ;
}

./test_trim 
   123  456 789 
123
456
789