Chapter 14. Boost.Array

The library Boost.Array defines the class template boost::array in boost/array.hpp. boost::array is similar to std::array, which was added to the standard library with C++11. You can ignore boost::array if you work with a C++11 development environment.

With boost::array, an array can be created that exhibits the same properties as a C array. In addition, boost::array conforms to the requirements of C++ containers, which makes handling such an array as easy as handling any other container. In principle, one can think of boost::array as the container std::vector, except the number of elements in boost::array is constant.

Example 14.1. Various member functions of boost::array

  1. #include <boost/array.hpp>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. int main()
  6. {
  7. typedef boost::array<std::string, 3> array;
  8. array a;
  9. a[0] = "cat";
  10. a.at(1) = "shark";
  11. *a.rbegin() = "spider";
  12. std::sort(a.begin(), a.end());
  13. for (const std::string &s : a)
  14. std::cout << s << '\n';
  15. std::cout << a.size() << '\n';
  16. std::cout << a.max_size() << '\n';
  17. }

As seen in Example 14.1, using boost::array is fairly simple and needs no additional explanation since the member functions called have the same meaning as their counterparts from std::vector.