C++ data structures


C/C++ arrays allow you to define variables that can store data items of the same type, but structure is another user-defined data type available in C++ that allows you Stores different types of data items. The

structure is used to represent a record. Suppose you want to track the dynamics of books in the library. You may need to track the following attributes of each book:

  • Title

  • Author

  • Subject

  • Book ID

Define a structure

In order to define a structure, you must use the struct statement. The struct statement defines a new data type containing multiple members. The format of the struct statement is as follows:

struct [structure tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];

structure tag is optional, and each member definition is a standard variable definition. , such as int i; or float f; or other valid variable definitions. At the end of the structure definition, before the last semicolon, you can optionally specify one or more structure variables. Here's how to declare a Book structure:

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}book;

Accessing structure members

To access the members of a structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of structure type. The following example demonstrates the use of structures:

#include <iostream>
#include <cstring>
 
using namespace std;
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 输出 Book1 信息
   cout << "Book 1 title : " << Book1.title <<endl;
   cout << "Book 1 author : " << Book1.author <<endl;
   cout << "Book 1 subject : " << Book1.subject <<endl;
   cout << "Book 1 id : " << Book1.book_id <<endl;

   // 输出 Book2 信息
   cout << "Book 2 title : " << Book2.title <<endl;
   cout << "Book 2 author : " << Book2.author <<endl;
   cout << "Book 2 subject : " << Book2.subject <<endl;
   cout << "Book 2 id : " << Book2.book_id <<endl;

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700

Structures as function parameters

You Structures can be used as function parameters, and the parameter passing method is similar to other types of variables or pointers. You can access structure variables in the same way as in the above example:

#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books book );

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 输出 Book1 信息
   printBook( Book1 );

   // 输出 Book2 信息
   printBook( Book2 );

   return 0;
}
void printBook( struct Books book )
{
   cout << "Book title : " << book.title <<endl;
   cout << "Book author : " << book.author <<endl;
   cout << "Book subject : " << book.subject <<endl;
   cout << "Book id : " << book.book_id <<endl;
}

When the above code is compiled and executed, it produces the following results:

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

Pointer to structure

You can define a pointer to a structure in a similar way as you define a pointer to a variable of other types, as shown below:

struct Books *struct_pointer;

Now you can store the address of the structure variable in the pointer variable defined above. To find the address of a structure variable, put the & operator in front of the structure name, as follows:

struct_pointer = &Book1;

To access a member of a structure using a pointer to the structure, you must use the -> operator , as shown below:

struct_pointer->title;

Let us rewrite the above example using structure pointer, which will help you understand the concept of structure pointer:

#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books *book );

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book */
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 通过传 Book1 的地址来输出 Book1 信息
   printBook( &Book1 );

   // 通过传 Book2 的地址来输出 Book2 信息
   printBook( &Book2 );

   return 0;
}
// 该函数以结构指针作为参数
void printBook( struct Books *book )
{
   cout << "Book title : " << book->title <<endl;
   cout << "Book author : " << book->author <<endl;
   cout << "Book subject : " << book->subject <<endl;
   cout << "Book id : " << book->book_id <<endl;
}

When the above code is compiled and When executed, it produces the following result:

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

typedef Keywords

Here is a simpler way of defining a structure, you can give the type you create an "alias". For example:

typedef struct
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
}Books;

Now, you can directly use Books to define variables of type Books without using the struct keyword. The following is an example:

Books Book1, Book2;

You can use the typedef keyword to define a non-structural type, as follows:

typedef long int *pint32;
 
pint32 x, y, z;

x, y and z all point to long integers Pointer of type long int.