[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppBoostArray
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:
More advanced example using for-loops:
Example using std::for_each:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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:
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; }
- include <boost/array.hpp>
More advanced example using for-loops:
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; }
- include <iostream>
- include <algorithm>
- include <boost/array.hpp>
Example using std::for_each:
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()); }
- include <iostream>
- include <algorithm>
- include <boost/array.hpp>
Array links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
