05. C++ Array

Posted by Yulai Blog on November 5, 2015

Array

  • <boost/array.hpp>
  • boost::array<int,4> a = { 1, 2, 3 };
  • std::array is (as of C++11) part of the C++ standard
  • use std::array instead of boost::array if you are using C++11
#include <iostream>
#include <array>

void cpp_11_array()
{
    std::array<int, 4> a = {11, 22, 33, 44};
    
    for (int i=0; i < a.size(); ++i)
    {
        std::cout << a.at(i);
    }
    
    for (std::array<int, 4>::const_iterator it = a.begin(); it != a.end(); ++it)
    {
        std::cout << *it;
    }
}
#include <iostream>
#include <boost/array.hpp>

int main()
{
    boost::array<int, 4> a = {1, 2, 3, 4};

    for (size_t i=0; i < a.size(); ++i)
    {
        std::cout << a.at(i);
    }

    for (boost::array<int, 4>::const_iterator it = a.begin(); it != a.end(); ++it)
    {
        std::cout *it;
    }

    return 0;
}