The Built-In Array
Using COUNTOF implies that you are using C-style arrays, which in modern C++, have mostly been traded in for std::vector and other container classes. Why would you want to use an array? Most of the remaining applications come from two features of built-in arrays: They can use a C-style initializer list, and their size is fixed at compile time, which in some cases allows additional static checking and optimization. When these features are needed, and when the dynamic flexibility of container classes is not, arrays become an attractive option. (If, on the other hand, you find yourself needing to use new[] and delete[], or to track how many elements are "really" being used in an array, then container classes will usually be a better choice. They manage their own memory, track their own size, can grow as needed, and generally take care of the low-level details so that you can focus on the bigger picture.)
Initializer Lists
Unlike most container classes, arrays are allowed to use an initializer list:
// OK: long a[] = { 2, 3, 5, 7 }; // not allowed: vector<long> v = { 2, 3, 5, 7 };
You can explicitly add each item to a container:
v.push_back(2); v.push_back(3); v.push_back(5); v.push_back(7);
However, initializing containers "by hand" is tedious and repetitive, and when programming seems tedious and repetitive, it's usually a sign that you are doing something the hard way. Another way to initialize a container is to first create an array using an initializer list, then initialize the container from the array:
vector<long> v(a, a + COUNTOF(a));
This works because an array is convertible to a pointer to its first element, and pointers qualify as iterators. You can also use arrays with STL algorithms. For example, if a were not already sorted, you could sort it like this:
std::sort(a, a + COUNTOF(a));
Notice that I never explicitly specify the number of elements in the array. The compiler gives the array whatever size is needed to hold the initializer-list, then I infer that size using COUNTOF. There is no opportunity to get the size wrong, and if I later add another item to the list and recompile, everything else will be updated automatically.