The C++ Standard Template Library

Table of Contents


C++ comes with a bunch of data structures we can use to store sets of data. Each of these have different uses and work better for different designs. They all have documentation available online so you can learn more about how these structures work there as well.


1. Without the STL: Traditional fixed-length array

One of the first ways you may have learned to store a sequence of data is with a traditional array in C++. These aren't "smart" at all, cannot be resized, and you either have to manually keep track of the size of the array or do a calculation whenever you want to find the size.

They come in this form:

const int ARRAY_SIZE = 100;
int itemCount = 0;
int myArray[ ARRAY_SIZE ];

The ARRAY_SIZE named constant and the itemCount variable are optional, however, we have to manually keep track of the size of the array and how many items are stored within it - it won't take care of that for us.

Reading and writing values to this array is simple:

cout << "Enter a number: ";
cin >> myArray[ 0 ];
cout << "Item #0 is " << myArray[0] << endl;

And iterating through it requires, again, keeping track of the amount of items we've stored in the array with some kind of variable:

for ( int i = 0; i < itemCount; i++ )
{
  cout << i << " = " << myArray[i] << endl;
}

2. Without the STL: Dynamic array

We can create dynamic arrays by using pointers. This allows us to define the size of the array at runtime, instead of defining the size at compile-time like with our traditional fixed-length array. The downside is manually managing the memory - making sure to free whatever we have allocated.

int itemCount = 0;
int arraySize;

cout << "Enter an array size: ";
cin >> arraySize;

int* myArray = new int[ arraySize ];

Reading and writing values to the array is the same as with the traditional array. We just have to make sure to free the memory before the program ends:

delete [] myArray;

3. std::array

Documentation: https://cplusplus.com/reference/array/array/

he array from the STL is basically a class wrapping our traditional array. It allows us to use the array as an object with basic functions, so that we don't have to keep track of as much.

Declaration

Accessing the size

Adding data

Accessing data


4. std::vector

Documentation: https://cplusplus.com/reference/vector/vector/

Declaration

Accessing the size

Adding data

Accessing data


5. std::list

Documentation: https://cplusplus.com/reference/list/list/

Declaration

Accessing the size

Adding data

Accessing data


6. std::stack

Documentation: https://cplusplus.com/reference/stack/stack/

Declaration

Accessing the size

Adding data

Accessing data


7. std::queue

Documentation: https://cplusplus.com/reference/queue/queue/

Declaration

Accessing the size

Adding data

Accessing data


8. std::map

Documentation: https://cplusplus.com/reference/map/map/

Declaration

Accessing the size

Adding data

Accessing data


Author: Rachel Wil Sha Singh

Created: 2023-10-05 Thu 22:39

Validate