2.1. Using string and vector¶
Many useful things get done using the standard fundamental types. Often, however, we need to group a set of related types together. In order to group them together, we need some sort of container.
Two of these containers you should learn first: std::string and std::vector. They come with many useful functions that help solve many common problems. While there is some cost in using them, often the cost is worth the benefits in terms of reduced development time, code clarity, and other advantages.
Both string
and vector
are container classes.
That is,
their primary job is to make it easy to work with the
data stored inside the container.
The simplest ‘batch of data’ is a ‘batch of characters’
Another word for ‘batch of characters’ is ‘string’.
A string
is a container for data of a
single type: char.
A vector
can store data of any type: even types that you make up.
We will see how that is possible in a bit,
but first let’s focus on how to use these two important types.