You can create an array in C++, which gives you a bunch of values that are of the same type. For example, if you wanted to remember the names of ten people you could store them all in a single array instead of declaring ten different variables.

Declaring Arrays

Any data type in C++ can be used in an array:
std::string myArray[3];

The above code will create a string array with three values.

To assign a value to one of the elements of an array, just include the index, starting at zero:

std::string names[3];
names[0] = "Bob";
names[1] = "Jane";
names[2] = "Fred";

You can also assign all values at once when the array is declared:

std::string names[3] = {"Bob","Jane","Fred"};

Or you can fill the array with a single value:

std::string names[3] = {"Unnamed"};

Arrays can be created with any data type:

int myIntegerArray[5] = {46,12,74,90,23};

If you ever create an array of pointers make sure you initialize the whole thing to NULL or nullptr!

Iterating Through Arrays

We can iterate (walk through) an array to get each value using a for loop:
std::string names[3] = {"Bob","Jane","Fred"};

for (int n=0; n{

The std::size command returns the number of elements in the array. Don't confuse this with the sizeof function, which returns the size of the array's data in bytes.

C++ arrays are not resizable, and will always stay the same size they are declared as. To create resizable arrays in C++, we will need to learn about containers.