The Standard Template Library
◀ Why Start at Index 0 instead of Index 1?▶ Basic Container Properties Amazon
A student walked into the professor’s office.
“Professor Frankenstein, in class today you asked us to write a program to store all our data in an array and sort them without knowing the size of the array in advance.”
“Good day, Vika. Let’s see here.. You can use dynamic allocation to deal with size problem and you can use bubble sort, selection sort, quick sort, or any other type of sort you are comfortable with.”
“I know, but why not just use a vector to store data dynamically and use its sort function someone else has already written for us?”
“Uh…I never knew there is such thing as a vector.”
“It does exist. It is one of the containers defined in the STL.”
“STL? Containers?? What the $#@*%??? Nobody ever told me that…”
So you see, STL is a cool tool you should be equipped with in case you don’t have it yet.
The Standard Template Library, or STL, provides useful class templates called containers. A container is a way to store many objects of the same type, and different containers have different mechanisms to store and retrieve data.
The goal of using a container is to store multiple objects in one object and to manipulate them efficiently. The word “template” refers to STL’s ability to handle objects of many types, just like a class template does.
At times your program may need to store many objects of the same data type dynamically, and using a container may be a good idea. This chapter will cover some of the most common container types, including
vector,
list,
queue,
stack, and
set.
In my opinion,
vector is the most useful container because it allows the size of its objects to vary dynamically, growing as elements are added and shrinking as elements are removed. It also supports random access to elements unlike
stack or
queue. Finally, it is very easy to use.
Therefore, make sure you know at least vector.
Next we’ll look at the basic properties of a container!
◀ Why Start at Index 0 instead of Index 1?▶ Basic Container Properties