Data structure and structure

C++ data structure

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

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

  • Title: Title
  • Author: Author
  • Subject: category
  • Book ID: Book ID

Define structure

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

struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

type_name  is the name of the structure type;

member_type1 member_name1  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 specify one or more structure variables, which is optional.

The following is to declare a structure type  Books , the variable is  book :

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

Access structure member

To access the members of the 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.

The following example demonstrates the usage of the structure:

#include <iostream>
#include <cstring>
 
using namespace std;
 
// 声明一个结构体类型 Books
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
   // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Nowcoder");
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Nowcoder");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   cout << "第一本书标题 : " << Book1.title <<endl;
   cout << "第一本书作者 : " << Book1.author <<endl;
   cout << "第一本书类目 : " << Book1.subject <<endl;
   cout << "第一本书 ID : " << Book1.book_id <<endl;
 
   // 输出 Book2 信息
   cout << "第二本书标题 : " << Book2.title <<endl;
   cout << "第二本书作者 : " << Book2.author <<endl;
   cout << "第二本书类目 : " << Book2.subject <<endl;
   cout << "第二本书 ID : " << Book2.book_id <<endl;
 
   return 0;
}

The example defines a structure similar to Books and its two variables Book1 and Book2. When the above code is compiled and executed, it will produce the following results:

第一本书标题 : C++ 教程
第一本书作者 : Nowcoder
第一本书类目 : 编程语言
第一本书 ID : 12345
第二本书标题 : CSS 教程
第二本书作者 : Nowcoder
第二本书类目 : 前端技术
第二本书 ID : 12346

Structure as function parameter

The structure can be used as a function parameter, and the parameter passing method is similar to other types of variables or pointers.

You can use the method in the above example to access structure variables:

#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books book );
 
// 声明一个结构体类型 Books
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
    // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Nowcoder");
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Nowcoder");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   printBook( Book1 );
 
   // 输出 Book2 信息
   printBook( Book2 );
 
   return 0;
}
void printBook( struct Books book )
{
   cout << "书标题 : " << book.title <<endl;
   cout << "书作者 : " << book.author <<endl;
   cout << "书类目 : " << book.subject <<endl;
   cout << "书 ID : " << book.book_id <<endl;
}

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

书标题 : C++ 教程
书作者 : Nowcoder
书类目 : 编程语言
书 ID : 12345
书标题 : CSS 教程
书作者 : Nowcoder
书类目 : 前端技术
书 ID : 12346

Pointer to structure

You can define pointers to structures in a similar way to defining pointers to other types of variables, as follows:

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 shown below:

struct_pointer = &Book1;

In order to use a pointer to the structure to access the members of the structure, you must use the -> operator as follows:

struct_pointer->title;

Let us use structure pointers to rewrite the above example, which will help you understand the concept of structure pointers:

#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( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
    // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Nowcoder");
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Nowcoder");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 通过传 Book1 的地址来输出 Book1 信息
   printBook( &Book1 );
 
   // 通过传 Book2 的地址来输出 Book2 信息
   printBook( &Book2 );
 
   return 0;
}
// 该函数以结构指针作为参数
void printBook( struct Books *book )
{
   cout << "书标题  : " << book->title <<endl;
   cout << "书作者 : " << book->author <<endl;
   cout << "书类目 : " << book->subject <<endl;
   cout << "书 ID : " << book->book_id <<endl;
}

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

书标题  : C++ 教程
书作者 : Nowcoder
书类目 : 编程语言
书 ID : 12345
书标题  : CSS 教程
书作者 : Nowcoder
书类目 : 前端技术
书 ID : 12346

typedef keyword

The following is a simpler way to define the structure, you can take an "alias" for the created type. E.g:

typedef struct Books
{
   char title[50];
   char author[50];
   char subject[100];
   int  book_id;
}BOOKS;   //大写
BOOKS book1, 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 non-structural types as follows:

typedef long int *pint32;
 
pint32 x, y, z;
x, y 和 z 都是指向长整型 long int 的指针

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/111874021