Learning C++: Part 8: Complex Data Types

Learning C++


Complex Data Types

Between using structs to define new data types, objects to build more complex models, and pointers to manage locations and new variables, C++ can be extended in all manner of ways. And, in fact, the system libraries that nearly always come with C++ include many common ways to extend functionality for solving common problems and help with repetitive tasks.

<string>

This library provides functionality to work with strings. It contains ways to make them (constructors), delete them (destructors), and functions to work with them. Beyond the more basic usage of character arrays for representing strings, this library provides an easy-to-use version of string.

<vector>

vector is one of several types of containers provided by system libraries. It works like an array, providing random access, but new entries can be added and removed for it.

Vectors are a generic data type. That is, the type of data stored within it must be defined when the variable itself is defined. But, in being a container, as long as the data type is defined somewhere before its usage, it can be used in a vector

Vector use iterators, functionality that define positions within the structure. Because of their more dynamic nature, iterators are used to move through the containers.

<algorithm>

To use functions like find(), search(), and sort(), include use the <algorithm> library. While named files like <vector> contain those types, functions for working across or with generic data types are in <algorithm>.

<iterator>

The system library <iterator> is nearly always included with other generic data types like <vector>. In C++, an iterator is a method to navigate the generic data types.

Iterators act like the functionality to access elements in an array, but are defined as functions. Basic iterators include begin(), the first entry in the container, and end(), the last entry. When there are no entries, both begin() and end() are set to each other.

Play with the example on Repl.it!