Boost C++常用库函数总结

Boost C++常用库函数总结

lexical_cast(...)

#include "boost/algorithm/string.hpp"
using namespace boost;
// 更加方便的类型转换
/*************** API ****************/
lexical_cast<Dtype>(...);
bad_lexical_cast;
/*************** 例程 ****************/
int main(int argc, char * argv[])
{
    using boost::lexical_cast;
    using boost::bad_lexical_cast;

    std::vector<short> args;

    while(*++argv)
    {
        try
        {
            args.push_back(lexical_cast<short>(*argv));
        }
        catch(bad_lexical_cast &)
        {
            args.push_back(0);
        }
    }
    ...
}

shared_ptr

#include <boost/shared_ptr.hpp>

/*
 *The shared_ptr class template stores a pointer to a dynamically allocated object, typically with a C++ new-expression.
 *The object pointed to is guaranteed to be deleted when the last shared_ptr pointing to it is destroyed or reset
**/
using boost::shared_ptr;
/*************** 例程 ****************/
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <boost/shared_ptr.hpp>

//  The application will produce a series of
//  objects of type Foo which later must be
//  accessed both by occurrence (std::vector)
//  and by ordering relationship (std::set).

struct Foo
{ 
  Foo( int _x ) : x(_x) {}
  ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
  int x;
  /* ... */
};

typedef boost::shared_ptr<Foo> FooPtr;

struct FooPtrOps
{
  bool operator()( const FooPtr & a, const FooPtr & b )
    { return a->x > b->x; }
  void operator()( const FooPtr & a )
    { std::cout << a->x << "\n"; }
};

int main()
{
  std::vector<FooPtr>         foo_vector;
  std::set<FooPtr,FooPtrOps>  foo_set; // NOT multiset!

  FooPtr foo_ptr( new Foo( 2 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset( new Foo( 1 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset( new Foo( 3 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset ( new Foo( 2 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  std::cout << "foo_vector:\n";
  std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
  
  std::cout << "\nfoo_set:\n"; 
  std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() );
  std::cout << "\n";

//  Expected output:
//
//   foo_vector:
//   2
//   1
//   3
//   2
//   
//   foo_set:
//   3
//   2
//   1
//
//   Destructing a Foo with x=2
//   Destructing a Foo with x=1
//   Destructing a Foo with x=3
//   Destructing a Foo with x=2
   
  return 0;
}
/**
 *Note that after the containers have been populated, some of the shared_ptr objects will have a use count of 1 rather than 
 *a use count of 2, since the set is a std::set rather than a std::multiset, and thus does not contain duplicate entries. 
 *Furthermore, the use count may be even higher at various times while push_back and insert container operations are 
 *performed. More complicated yet, the container operations may throw exceptions under a variety of circumstances. Getting 
 *the memory management and exception handling in this example right without a smart pointer would be a nightmare.
**/

bind(…)

#include <boost/bind.hpp>
boost::bind(...);

/**
 *boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function 
 *objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value 
 *or route input arguments into arbitrary positions. bind does not place any requirements on the function object; in 
 *particular, it does not need the result_type, first_argument_type and second_argument_type standard typedefs.
**/
/*************** Using bind with functions and function pointers ****************/
int f(int a, int b)
{
    return a + b;
}

int g(int a, int b, int c)
{
    return a + b + c;
}
std::bind1st(std::ptr_fun(f), 5)(x);   // f(5, x)
bind(f, 5, _1)(x);                     // f(5, x)

bind(f, _2, _1)(x, y);                 // f(y, x)
bind(g, _1, 9, _1)(x);                 // g(x, 9, x)
bind(g, _3, _3, _3)(x, y, z);          // g(z, z, z)
bind(g, _1, _1, _1)(x, y, z);          // g(x, x, x)

/*************** Using bind with function objects ****************/
...
/*************** Using bind with pointers to members ****************/
...
/*************** Using nested binds for function composition ****************/
...

split(…)

#include <boost/algorithm/string.hpp>


/*************** 例程 ****************/
string str1("hello abc-*-ABC-*-aBc goodbye");
typedef vector< string > split_vector_type;
split_vector_type SplitVec; // #2: Search for tokens
boost::split( SplitVec, str1, boost::is_any_of("-*"), boost::algorithm::token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }

function

thread_specific_ptr

/**
 *boost::thread_specific_ptr provides a portable mechanism for thread-local storage that works on all compilers ]
 *supported by Boost.Thread. Each instance of boost::thread_specific_ptr represents a pointer to an object (such as 
 *errno) where each thread must have a distinct value. The value for the current thread can be obtained using the get() 
 *member function, or by using the * and -> pointer deference operators. Initially the pointer has a value of NULL in 
 *each thread, but the value for the current thread can be set using the reset() member function.
**/
Table of Contents