0% found this document useful (0 votes)
8 views

DSA-Chapter-5.0-2024

Graphs

Uploaded by

roinieva22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DSA-Chapter-5.0-2024

Graphs

Uploaded by

roinieva22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Structures and Algorithms

Chapter 5.0: Introduction to Type Aliases (C++ Feature)

Since Chapter 3, we’ve started dealing with more complex data structures which can get
lengthy to declare and might be difficult to read. Let’s look back at the solveMaze function
in Chapter 4.0:
std::vector<Point> solveMaze(
const std::vector<std::string>& maze,
char wall,
char startChar,
char endChar
){
Point start = findPosition(maze, startChar);
Point end = findPosition(maze, endChar);

std::vector<Point> path;
std::vector<std::vector<bool>> seen(maze.size(), std::vector<bool>(maze[0].size
(), false));

walk(maze, wall, start, end, seen, path);


return path;
}

Some of the data types are quite lengthy right? It would be nice if we could replace some of
them with shorter names.
Fortunately, we can. It’s called type alias.
Type alias is a feature in C++ that allows you to create an alternative name, or alias, for a
data type. It's a convenient way to make code more readable and manageable, especially
when dealing with complex data structures.
A simple example of a type alias is:
typedef double distance;

A typedef (short for “type definition”) is an older way of creating an alias for a type.
In this example, distance becomes an alias for the double data type. This means that you
can now declare a double variable as follows:
distance meters = 4.5;

This can make your code more readable because the variable meters is clearly intended to
represent a distance.
Typedefs are still in C++ for backwards compatibility reasons, but they have been largely
replaced by type aliases in modern C++.
C++11 introduced a new way to create type aliases using the using keyword. The previous
example can be rewritten as:
using distance = double;

You should prefer type aliases over typedefs.

You might remember that by the end of Chapter 0, the using namespace directive was
introduced and it was explained why it is considered bad practice. It is important to
recognize that this is a completely different usage of the using keyword. Here, it's used
to create an alias for a complex or templated type, making your code cleaner and easier to
understand.

Type aliasing is most powerful when used with more complex types. For example, if you're
working with a complex data structure like a map of strings to vectors of integers, the
declaration can get quite long:
std::map<std::string, std::vector<int>> my_map;

Using a type alias, you can simplify this as follows:


using MapVector = std::map<std::string, std::vector<int>>;
MapVector my_map;

This makes your code cleaner and easier to read. It also makes it easier to change the
underlying data type later on. If you decide to change your map to hold vectors of floats
instead of integers, you only need to change the type alias, not every instance in your code.
In modern C++, the convention is to name type aliases (or any other type) that you define
yourself starting with a capital letter, and using no suffix. This is to help differentiate the
names of types from the names of variables and functions (which start with a lower case
letter), and prevents naming collisions between them.
Type aliases can improve readability and make your code easier to understand, but they're
not strictly necessary. The choice to use them often comes down to personal preference or
coding style guidelines. For example, if you find that a particular type is complex and you're
using it frequently throughout your code, it might be a good idea to create a type alias for it.
Let’s go back to our Chapter 4 example and try to create some type aliases.
using Point = std::pair<int, int>;
using Maze = std::vector<std::string>;
using Visited = std::vector<std::vector<bool>>;
using Path = std::vector<Point>;

Path solveMaze(
Maze& maze,
char wall,
char start,
char end
){
Visited seen(maze.size(), std::vector<bool>(maze[0].size(), false));
Path path;
walk(maze, wall, start, end, seen, path);
return path;
}
It’s more readable now, right?

Note that type aliasing is not unique to C++. Many programming languages provide mechanisms
for creating type aliases, although the specific syntax and features may vary from one language
to another.

In conclusion, while type aliases can be a valuable tool in certain circumstances, it's
important to weigh the potential benefits against the possible drawbacks. They should be
used when they genuinely improve the readability and maintainability of the code, and
not as a general-purpose renaming mechanism. Always consider the context of the code,
the potential for confusion, and the impact on the development and maintenance processes
before deciding to use a type alias.

You might also like