Data structures
Data structures are fundamental components used to organize, manage and store data. In C++, those are the built-in arrays, the standard template library (STL) and user-defined structures. Depending on the situation, you can use a particular STL adapted to the task in hand, or build your own.
Arrays
In Cpp an array is a variable that can store multiple values of the same type by assigning a memory slot for those values. Arrays are statics assigned, which means that when they are created they will not allow more values inside, but they are mutable.
In order to declare an array you must declare the data type and declare the size of the array in brackets:
| |
Here:
- data type is
int - variable is
x - size
6
There several ways to declare an array, and we are not forced to use the full size of an array, it can handle empty spaces if they are not assigned. In the case you assign variables to an array without declaring the size the system will handle it automatically.
Array indexing
In C++, each element in an array is associated with a number. The number is known as an array index. We can access elements of an array by using those indices.
array[index];
| |
| |
Multidimensional arrays
We can create arrays of an array, known as a multidimensional array. It is helpful for multidimensional functions and for linear algebra operations.
Like a normal array, we can initialize a multidimensional array in more than one way.
| |
This method is not preferred. A better way to initialize this array with the same array elements is as follows:
| |
For a 3 dimension arrays it works in the same way and so on for n-dimension:
| |
Once again, This method is not preferred. A better way to initialize this three-dimension array with the same array elements is as follows:
| |
Strings
There is not strings-structures natively in Cpp, the strings are char arrays. There are two types of strings commonly used in C++ :
- C-strings (C-style Strings)
- Strings that are objects of string class (The Standard Library String Class)
C-strings
In C the collection of characters is stored in the form of arrays terminated with a null character.
String class
In Cpp there is an object string that can hold strings. These strings are not fixed length objects and can be extended as needed.
| |
| |
Like arrays, it is not necessary to use all the space allocated for the string.
An important difference between char and string is that for character single quotation marks (’’) is used, and for strings is used double quotation marks (" “).
The common functions that are used with the string class are.
| Function | Description |
|---|---|
find() | Find a substring in the string. |
rfind() | Find the last occurrence of a substring in the string. |
append() | Append to the string. |
insert() | Insert into the string. |
erase() | Erase characters from the string. |
replace() | Replace portions of the string. |
compare() | Compare two strings. |
Standard Template Library (STL)
Cpp provides a set of programming tools to implement algorithms and data structure. These data structures and algorithms implement general-purpose classes and functions that have been tested rigorously, and this package take the name as Standard Template Library or STL in short.
STL has 3 major components:
- Containers
- Iterators:
- Algorithms
STL Containers
The containers are objects that store data and organize them in a specific manner as required.
The STL containers can be classified into 2 types:
- Sequence:
- Array
- Vector
- Queue
- Deque
- List
- stack
- Association:
- Set
- Map
Array
It is a container class that encapsulates fixed size arrays it is similar to the arrays as it stores multiples values of similar type.
Declaration :
| |
where,
T - Type of element to be stored
N - Number of elements in the array
The initialization of an array can be done in two ways:
Method 1:
| |
Method 2:
| |
Arrays are mutable and indexed, you can access to all values and modify them using the index.
| |
| |
The common functions that are used with the string class are.
| Function | Description |
|---|---|
begin() | Return iterator to beginning. |
end() | Return iterator to beginning. |
swap() | Swap content. |
fill() | Fill the array with value. |
size() | Return size. |
max_size() | Return size. |
empty() | Return max size. |
Vector
It is a container to store elements of similar types. However, unlike arrays, the size of a vector can grow dynamically. Which means that we can change the vector size during the execution of a program.
Vectors follows the same logic that arrays.
Declaration :
| |
where,
T - Type of element to be stored
The initialization of an array can be done in two ways:
Method 1:
| |
Method 2:
| |
| |
The vector header file provides various functions that can be used to perform different operations on a vector.
| Function | Description |
|---|---|
size() | returns the number of elements present in the vector |
clear() | removes all the elements of the vector |
front() | returns the first element of the vector |
back() | returns the last element of the vector |
empty() | returns 1 (true) if the vector is empty |
capacity() | check the overall size of a vector |
List, Queue, Deque, and Stacks
They are containers that store elements in random, unrelated locations. To maintain sequential order, each element contains two links: one that points to the previous element and one that points to the next one. The main difference is as shown:
- List are accessed by anywhere
- Stacks (LIFO) are accessed by the tops (Firs-In, First-Out)
- Queue (FIFO) are accessed by the ends (First-In, First-Out)
- Deque by both ends (Double Ended Queue)
Map and set
They are associated containers that hold data in an ordered manner, mainly arranged in ascending order of their keys values. For the maps is stored a pair of data known as key-value pair where each key is unique while the associated value does not have to be unique, meanwhile the sets store unique elements (keys) of the same type in a sorted manner.