[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppBoostArray

(C++) boost::array

An array class from the Boost C++ library.

It is safer to use then a plain array, because it is not a pointer in disguise.

The use of a std::vector is recommened by default, but if you need not to use resizing, a boost::array might be faster: it does not need to be ready for a resizing operations, which saves overhead.

For doing mathematical operations on a sequence of values, you could also use a std::valarray.

Unlike a plain array, a boost::array can do range checking:

  1. include <boost/array.hpp>
int main() { const int size = 10; boost::array<double,size> myArray; for (int i=0; i!=size+1; ++i) myArray.at(i)=i; //Checks range for (int i=0; i!=size+1; ++i) myArray[i]=i; //Does not check range return 0; }


More advanced example using for-loops:
  1. include <iostream>
  2. include <algorithm>
  3. include <boost/array.hpp>
int main() { const int size = 10; boost::array<double,size> myArray; for (int i=0; i!=size; ++i) myArray[i]=i; for (int i=0; i!=size; ++i) std::cout << myArray[i] << std::endl; for (int i=0; i!=size; ++i) myArray[i]*=myArray[i]; for (int i=0; i!=size; ++i) std::cout << myArray[i] << std::endl; }


Example using std::for_each:
  1. include <iostream>
  2. include <algorithm>
  3. include <boost/array.hpp>
class Init { public: Init() : i(0) {} template <class T> void operator()(T& j) { j = i; i++; } private: int i; }; class Couter { public: Couter() {} void operator()(double& t) { std::cout << t << std::endl; } template <class T> void operator()(const T& t) { std::cout << t << std::endl; } }; class Squarer { public: Squarer() {} template <class T> void operator()(T& t) { t*=t; } }; int main() { boost::array<double,10> myArray; std::for_each(myArray.begin(),myArray.end(),Init()); std::for_each(myArray.begin(),myArray.end(),Couter()); std::for_each(myArray.begin(),myArray.end(),Squarer()); std::for_each(myArray.begin(),myArray.end(),Couter()); }


Array links



last edited (November 9, 2006) by bilderbikkel, Number of views: 5298, Current Rev: 8 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.