C++ Data Structures
Basics of creating custom data structures in C++
Overview:
Arrays are useful when a huge collection of data elements need to be stored in a single variable. However, the limitation on any array is that it can only store one type of value in one variable. For example, if you have a list of names of books, then you can effortlessly store all the name strings in a single array. But, if you have a list of books, and you need to store multiple books in a single variable with data like the name of the book, and the year it was published in, and any additional information to identify the book, then it’s practically impossible to store them in an array.
Wouldn’t it be awesome if we could create a data structure custom to our data storage needs?
Yes, yes it would. The solution to our problem is using struct. struct is a keyword that let’s us define data structures in C++. Data structures in C++ are variables that combine several data items of different types. Let’s solve our book problem above by learning how to use struct.
How to Define Structures:
A structure can be define using the following format:
struct type_name {
member_type member_name;
member_type member_name;
...
member_type member_name;
} object_names;
Let's look at each keyword more closely:
-
struct
is the tag used to define a structure. -
type_name
is the name we give our structure. This name is required when we declare a variable with our data structure. -
member_type
could be any native C++ data type or other structures that we can or have defined. We absolutely need to declare and define each data type we want to use for the members in our struct. -
member_name
s the variable name we need to provide for each of our member types. -
object_names
are the variable names of the data structure type that we just defined. Declaring variable names when defining the structure is optional because we can do so later as needed by the program.
So confusing!
Yeah, I know, but don’t worry. Let’s look at an example definition to get a better perspective on how to declare struct.
struct Books {
string title;
int year;
double price;
} book_1, book_2;
Following the syntax above, we now have created a data structure named Books, which stores three different data types. Furthermore, we declared book_1 and book_2, which are now instances of our data structure Books.
How to Access Structure Members:
Accessing a member of a structure is possible by using the member access operator (.). The member access operator is literally a period, also known as the dot operator and is also used in various other operations besides struct in C++. The operator is used between the structure variable name and the structure member that we are trying to access. Accessing, in this case connotes that we can mutate or print the values stored in the member variables. Let’s look at an example to better understand how to access structure members.
#
using namespace std;
struct Books {
string title;
int year;
double price;
};
int main() {
struct Books book_1, book_2;
book_1.title = "Hooked";
book_1.year = 2013;
book_1.price = 14.99;
book_2.title = "The Lean Startup";
book_2.year = 2011;
book_2.price = 13.99;
cout << "Book title: " << book_1.title << "\n";
cout << "Book year: " << book_1.year << "\n";
cout << "Book price: " << book_1.price << "\n";
cout << "Book title: " << book_2.title << "\n";
cout << "Book year: " << book_2.year << "\n";
cout << "Book price: " << book_2.price << "\n";
return 0;
}
When we execute the program above, we should see all the books printed to the console. The dot operator let’s us access individual members of all and any structure variable.
Structures as Function Arguments:
A structure comprises similar behaviors of any other variable in C++. This means that we can also pass our data structure variables around in functions like we can with any other variable. Let’s write a function for the program above that will incorporate this idea and make the printing less tedious.
#
using namespace std;
void printBook( struct Books book );
struct Books {
string title;
int year;
double price;
};
int main() {
struct Books book_1, book_2;
book_1.title = "Hooked";
book_1.year = 2013;
book_1.price = 14.99;
book_2.title = "The Lean Startup";
book_2.year = 2011;
book_2.price = 13.99;
printBook(book_1);
printBook(book_2);
return 0;
}
void printBook( struct Books book ) {
cout << "Book title: " << book.title << "\n";
cout << "Book year: " << book.year << "\n";
cout << "Book price: " << book.price << "\n";
}
Pointers to Structures:
Yes, we can use pointers with basically any data types in C++ and structures too can be pointed to by its own type of pointers. Declaration of a structure pointer variable is similar to any other data type. Apropos to our example above, a simple declaration of a structure pointer would be:
struct Books *ptr;
Now, we can store the address of a structure variable in the pointer variable ptr defined above. This is also similar to how we assign addresses to any other pointer variable.
ptr = &book_1;
Next step is to dereference the structure pointer variable. For that, we need to make use of a new operator called the arrow operator ( -> ).
ptr -> title
Using the arrow operator is also the same as using the dereference operator (*):
(*ptr).title
To see this in action, let’s rewrite the example above using pointers.
#
using namespace std;
void printBook( struct Books *book );
struct Books {
string title;
int year;
double price;
};
int main() {
struct Books book_1, book_2;
book_1.title = "Hooked";
book_1.year = 2013;
book_1.price = 14.99;
book_2.title = "The Lean Startup";
book_2.year = 2011;
book_2.price = 13.99;
printBook(&book_1);
printBook(&book_2);
return 0;
}
void printBook( struct Books *book ) {
cout << "Book title: " << book -> title << "\n";
cout << "Book year: " << book -> year << "\n";
cout << "Book price: " << book -> price << "\n";
}
In a nutshell:
Data structures in C++ is a way to define data if there is a need to store various data types in a single variable. In this tutorial, we learned how to define and use simple data structures using struct. However, struct is only an underlying idea for creating more complex data structures. It is also fundamental to defining classes in C++. Enjoy using struct in your C++ programs and stay tuned to learn how to implement classes using them.