Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

C++ Programming/Libraries/Boost

From Wikibooks, open books for an open world
<C++ Programming

TheBoost project (http://www.boost.org/) provides freepeer-reviewed,open sourcelibraries that extend the functionality of C++. Most of the libraries are licensed under theBoost Software License, designed to allow Boost to be used with both open andclosed source projects.

Many of Boost's founders are on theC++ standard committee and several Boost libraries have been accepted for incorporation into theTechnical Report 1 ofC++0x. Although Boost was begun by members of the C++ Standards Committee Library Working Group, participation has expanded to include thousands of programmers from the C++ community at large.

The emphasis is on libraries which work well with the C++ Standard Library. The libraries are aimed at a wide range of C++ users and application domains, and are in regular use by thousands of programmers. They range from general-purpose libraries likeSmartPtr, to OS Abstractions likeFileSystem, to libraries primarily aimed at other library developers and advanced C++ users, likeMPL.

A further goal is to establish "existing practice" and provide reference implementations so that Boost libraries are suitable for eventual standardization. Ten Boost libraries will be included in theC++ Standards Committee's upcomingC++ Standard Library Technical Report as a step toward becoming part of a future C++ Standard.

In order to ensure efficiency and flexibility, Boost makes extensive use oftemplates. Boost has been a source of extensive work and research intogeneric programming andmetaprogramming in C++.

Libraries

[edit |edit source]

Libraries by category

Concurrent programming
... TODO
  • Algorithms
  • Concurrent programming (threads)
  • Containers
    • array - Management of fixed-size arrays with STL container semantics
    • Boost Graph Library (BGL) - Generic graph containers, components and algorithms
    • multi-array - Simplifies creation of N-dimensional arrays
    • multi-index containers - Containers with built in indexes that allow different sorting and access semantics
    • pointer containers - Containers modeled after most standard STL containers that allow for transparent management of pointers to values
    • property map - Interface specifications in the form of concepts and a general purpose interface for mapping key values to objects
    • variant - A safe and generic stack-based object container that allows for the efficient storage of and access to an object of a type that can be chosen from among a set of types that must be specified at compile time.
  • Correctness andtesting
    • concept check - Allows for the enforcement of actual template parameter requirements (concepts)
    • static assert - Compile time assertion support
    • Boost Test Library - A matched set of components for writing test programs, organizing tests into test cases and test suites, and controlling their runtime execution
  • Data structures
  • Function objects andhigher-order programming
    • bind andmem_fn - General binders for functions, function objects, function pointers and member functions
    • function - Function object wrappers for deferred calls. Also, provides a generalized mechanism for callbacks
    • functional - Enhancements to the function object adapters specified in the C++ Standard Library, including:
    • hash - An implementation of the hash function object specified by the C++ Technical Report 1 (TR1). Can be used as the default hash function for unordered associative containers
    • lambda - In the spirit oflambda abstractions, allows for the definition of small anonymous function objects and operations on those objects at a call site, using placeholders, especially for use with deferred callbacks from algorithms.
    • ref - Provides utility class templates for enhancing the capabilities of standard C++ references, especially for use with generic functions
    • result_of - Helps in the determination of the type of a call expression
    • Signals2 - Managed signals and slots callback implementation
  • Generic programming
  • Graphs
  • Input/output
  • Interlanguage support (forPython)
  • Iterators
    • iterators
    • operators - Class templates that help with overloaded operator definitions for user defined iterators and classes that can participate in arithmetic computation.
    • tokenizer - Provides a view of a set of tokens contained in a sequence that makes them appear as a container with iterator access
  • Math and Numerics
  • Memory
    • pool - Provides a simple segregated storage based memory management scheme
    • smart_ptr - A collection of smart pointer class templates with different pointee management semantics
      • scoped_ptr - Owns the pointee (single object)
      • scoped_array - Like scoped_ptr, but for arrays
      • shared_ptr - Potentially shares the pointer with other shared_ptrs. Pointee is destroyed when last shared_ptr to it is destroyed
      • shared_array - Like shared_ptr, but for arrays
      • weak_ptr - Provides a "weak" reference to an object that is already managed by a shared_ptr
      • intrusive_ptr - Similared to shared_ptr, but uses a reference count provided by the pointee
    • utility - Miscellaneous support classes, including:
      • base from member idiom - Provides a workaround for a class that needs to initialize a member of a base class inside its own (i.e., the derived class') constructor's initializer list
      • checked delete - Check if an attempt is made to destroy an object or array of objects using a pointer to an incomplete type
      • next and prior functions - Allow for easier motion of a forward or bidirectional iterator, especially when the results of such a motion need to be stored in a separate iterator (i.e., should not change the original iterator)
      • noncopyable - Allows for the prohibition of copy construction and copy assignment
      • addressof - Allows for the acquisition of an object's real address, bypassing any overloads of operator&(), in the process
      • result_of - Helps in the determination of the type of a call expression
  • Miscellaneous
  • Parsers
  • Preprocessor metaprogramming
  • String and text processing
    • lexical_cast - Type conversions to/from text
    • format - Type safe argument formatting according to a format string
    • iostreams - C++ streams and stream buffer assistance for new sources/sinks, filters framework
    • regex - Support for regular expressions
    • Spirit - An object-oriented recursive-descent parser generator framework
    • string algorithms - A collection of various algorithms related to strings
    • tokenizer - Allows for the partitioning of a string or other character sequence intotokens
    • wave - Standards conformant implementation of the mandatedC99 / C++ pre-processor functionality packed behind an easy to use interface
  • Template metaprogramming
    • mpl - A general purpose high-level metaprogramming framework of compile-time algorithms, sequences and metafunctions
    • static assert - Compile time assertion support
    • type traits - Templates that define the fundamental properties of types
  • Workarounds for broken compilers

The current Boost release contains 87 individual libraries, including the following three:

noncopyable

[edit |edit source]

Theboost::noncopyable utility class thatensures that objects of a class are never copied.

classC:boost::noncopyable{...};

Linear algebra – uBLAS

[edit |edit source]

Boost includes theuBLASlinear algebra library (faster alternative libraries include armadillo and eigen), withBLAS support for vectors and matrices. uBlas supports a wide range of linear algebra operations, and has bindings to some widely used numerics libraries, such as ATLAS, BLAS and LAPACK.

  • Example showing how to multiply a vector with a matrix:
#include<boost/numeric/ublas/vector.hpp>#include<boost/numeric/ublas/matrix.hpp>#include<boost/numeric/ublas/io.hpp>#include<iostream>usingnamespaceboost::numeric::ublas;/* "y = Ax" example */intmain(){vector<double>x(2);x(0)=1;x(1)=2;matrix<double>A(2,2);A(0,0)=0;A(0,1)=1;A(1,0)=2;A(1,1)=3;vector<double>y=prod(A,x);std::cout<<y<<std::endl;return0;}

Generating random numbers – Boost.Random

[edit |edit source]

Boost provides distribution-independentpseudorandom number generators and PRNG-independent probability distributions, which are combined to build a concrete generator.

#include<boost/random.hpp>#include<ctime>usingnamespaceboost;doubleSampleNormal(doublemean,doublesigma){// Create a Mersenne twister random number generator// that is seeded once with #seconds since 1970staticmt19937rng(static_cast<unsigned>(std::time(0)));// select Gaussian probability distributionnormal_distribution<double>norm_dist(mean,sigma);// bind random number generator to distribution, forming a functionvariate_generator<mt19937&,normal_distribution<double>>normal_sampler(rng,norm_dist);// sample from the distributionreturnnormal_sampler();}

SeeBoost Random Number Library for more details.

Multi-threading – Boost.Thread

[edit |edit source]

Example code that demonstrates creation of threads:

#include<boost/thread/thread.hpp>#include<iostream>usingnamespacestd;voidhello_world(){cout<<"Hello world, I'm a thread!"<<endl;}intmain(intargc,char*argv[]){// start two new threads that calls the "hello_world" functionboost::threadmy_thread1(&hello_world);boost::threadmy_thread2(&hello_world);// wait for both threads to finishmy_thread1.join();my_thread2.join();return0;}

See alsoThreading with Boost - Part I: Creating Threads

Thread locking

[edit |edit source]

Example usage of a mutex to enforce exclusive access to a function:

#include<iostream>#include<boost/thread.hpp>voidlocked_function(){// function access mutexstaticboost::mutexm;// wait for mutex lockboost::mutex::scoped_locklock(m);// critical section// TODO: Do something// auto-unlock on return}intmain(intargc,char*argv[]){locked_function();return0;}

Example of read/write locking of a property:

#include<iostream>#include<boost/thread.hpp>/** General class for thread-safe properties of any type. */template<classT>classlock_prop:boost::noncopyable{public:lock_prop(){}/** Set property value. */voidoperator=(constT&v){// wait for exclusive write accessboost::unique_lock<boost::shared_mutex>lock(mutex);value=v;}/** Get property value. */Toperator()()const{// wait for shared read accessboost::shared_lock<boost::shared_mutex>lock(mutex);returnvalue;}private:/// Property value.Tvalue;/// Mutex to restrict accessmutableboost::shared_mutexmutex;};intmain(){// read/write locking propertylock_prop<int>p1;p1=10;inta=p1();return0;}
Retrieved from "https://en.wikibooks.org/w/index.php?title=C%2B%2B_Programming/Libraries/Boost&oldid=4112463"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp