Learning C++: The STL and the Array Class

Mike McMillan
Level Up Coding
Published in
5 min readApr 4, 2020

--

Photo by Nico Giras on Unsplash

C++’s built-in (primitive) array has a few deficiencies that make it hard to use in many programs. One of the major problems with the primitive array is there isn’t a function that returns the size of the array. This means that when you need the size of the array to do something like control a for loop, you must use a variable or a constant that is storing the array’s size. Another problem with the primitive array is you can’t use pass the array to a function you also have to pass its size. You can’t use a range for loop with a primitive array in a function because the function can’t access the information it needs to set up an iterator.

A solution to using the primitive array is to use the array class that is part of the Standard Template Library (STL) and its set of containers. In this article I’ll cover how to use this class in your C++ programs.

Features of the array Class

The array class is a template class found in the STL. It is considered as one of the STL container classes so it has many of the same features as other STL containers such as the vector class. The array class should be chosen over other STL containers if you need a fixed number sequence because, like the primitive array, the array class provides random access and the memory used to store its elements is allocated on the stack…

--

--

Mike McMillan writes about computer programming and running. See more of his writing and courses at https://mmcmillan.gumroad.com.