Updated March 31, 2023
Definition of C++ Data Structures
If we talk in general terms, the data structure is basically a structure of how the data should be stored in memory in order to have an easy organization of it. Different types of data structures are used according to the requirements of how the programmer wants to keep the records. As we know that C++ is a high-level language having advanced features of classes and objects. It provides many data structures to deal with various situations like Arrays, Linked List, Stacks, Queues, Binary Tree, etc. C++ allows the user to create a user-defined data structure that can keep the records of data members having different data types.
Syntax:
Below given is the basic syntax of the user-defined data structure having data of different types:
structstructure_name {
member_datatype1 member_name1;
member_datatype2 member_name2;
member_datatype3 member_name3;
member_datatype4 member_name4;
..
..
..
} obj_name;
where,
- struct: keyword used to define the structure
- structure_name: name of the user-defined structure (it is optional)
- member_datatype(n): datatype of the structure member(n)
- member_name(n): name of the structure member(n)
- obj_name: name of the object of the user-defined structure that will have this structure
How do Data Structures work in C++?
Data structures in C++ are categorized into 2 broad categories as Primitive and non-primitive/user-defined data structures. Primitive data structures are the ones that are already defined in C++ libraries like int, float, double, String, etc. User-defined are the one which is created by the user according to the requirements of the programmer. Basically, in real-life scenarios, we need a structure that can hold the data members having different data types. For example, if we want to store the address of Employees, we need the data members which is house no. (int), Street name (char), City (char). As all the 3 data members have different data types, we need to create a user-defined data structure with the name ‘Address’ with all the data members defined in it along with their datatype.
structAddress{
inthouse_no;
char street[80];
char city[180];
} add1, add2;
In the above code, we have the structure name ‘Address’ with the data members house_no, street, city. ‘add1’ and ‘add2’ are the objects of the structure type Address. Though the name of the structure ‘Address’ is optional, then we need to specify at least 1 object before the end of the semicolon of the structure definition.
If we want to define the structure variable/ object_name somewhere apart from the definition, we need to use the keyword ‘struct’ for this:
struct Address add1;
Accessing the members in user defined data structure in C++: Data members of the data structure are accessed either to extract their value or assign them the value to perform further tasks. Data members of the structure are accessed using the dot (.) operator in C++ which is also known as the member access variable. In order to access the data member using the (.) operator, we need to use it between the name of the structure variable and the name of the data member. The above data member ‘house_no.’ is accessed in the way given below:
add1.house_no. = 12;
Examples of C++ Data Structures
Below given are some of the examples of the user-defined data structures in the C++ code:
Example #1
Defining and accessing the structure in C++. Code:
// Creating a user defined structure 'Address'
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct address {
inthouse_no;
string street;
string city;
} add1, add2;
intmain() {
//We can also declare the objects of structure here using 'struct address add1;'
// assigning the values to the add1 data members
add1.house_no = 10;
add1.street= "Ashok Vihar";
add1.city= "Delhi";
// assigning the values to the add2 data members
add2.house_no = 12;
add2.street = "Pitampura";
add2.city= "Delhi";
// Printing the above information on console
cout<< "Details of Address1 are as follows: "<<endl;
cout<< "Address 1 -- house_no : " << add1.house_no <<endl;
cout<< "Address 1 -- street : " << add1.street <<endl;
cout<< "Address 1 -- city : " << add1.city <<endl;
cout<< "Details of Address2 are as follows: "<<endl;
cout<< "Address 2 -- house_no : " << add2.house_no <<endl;
cout<< "Address 2 -- street : " << add2.street <<endl;
cout<< "Address 2 -- city : " << add2.city <<endl;
return 0;
}
Output:
Explanation: In the above code, we have created a structure named ‘address’ and defined house_no., street, and city as its data members. We have created ‘add1’ and ‘add2’ as its objects. In order to assign the values to the data members, we have first accessed that data member through the (.) operator. Similarly, in order to print those values on the console, the (.) operator is used to access them and ‘cout’ to print them.
Example #2
Defining the structure and Using an array of it in C++.
Code:
// Creating an array of user defined structure 'address'
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct address {
inthouse_no;
string street;
string city;
};
intmain() {
inti;
struct address add[2];
for (i=0;i<2;i++)
{
cout<< "Address of " << i+1 << " employee"<<endl;
cout<< "enter house_no"<<endl;
cin>>add[i].house_no;
cout<< "enter street"<<endl;
cin>>add[i].street;
cout<< "enter city"<<endl;
cin>>add[i].city;
}
// Printing the above information on console
for (i=0; i<2;i++)
{
cout<< "Details of Address "<< i+1<<" are as follows: "<<endl;
cout<< "house_no : " << add[i].house_no<<endl;
cout<< "street : " << add[i].street <<endl;
cout<< "city : " << add[i].city <<endl;
}
return 0;
}
Output:
Explanation: In the above code, we have created an array of the user-defined structure ‘address’. Practically we do not assign the values of data members, they are dynamically entered by the user. Structure array is created in a similar manner as the normal array in C++ Array size is 2 and the details are entered by the user. Data members are accessed using the (.) operator.
Conclusion
The above explanation clearly defines the data structures in C++ and how they work in C++ code. Once created the user defined data structure, they work similarly to the primitive data structures in C++. We can also pass them as an argument in the function and can define pointers to these structures. So it is very important to understand them thoroughly before working on them.
Recommended Articles
This is a guide to C++ Data Structures. Here we also discuss the introduction and how data structures work in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –