编程知识 cdmana.com

C + + basic knowledge: C + + data structure

C/C++ Learning skirt of 【 712   two eight four   Seven Zero Five  】, No matter you are Xiaobai is still an advanced person , If you want to change your career or join it, you can learn about it and learn from it together ! There are development tools in the skirt , A lot of dry goods and technical information sharing !

 

C/C++ Arrays allow you to define variables that can store items of the same type , however structure yes C++ Another user-defined available data type in , It allows you to store different types of data items .

Structure is used to represent a record , Suppose you want to track the movement of books in the library , You may need to track the following properties for each book :

  • Title : title
  • Author : author
  • Subject : category
  • Book ID : Of the book ID

Defining structure

To define the structure , You must use  struct  sentence .struct Statement defines a new data type with multiple members ,struct The format of the 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  It's the standard variable definition , such as  int i;  perhaps  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 , This is optional . Here's how to declare a struct type  Books, Variable is  book

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

Access structure members

To access members of the structure , We use Member access operators (.). Member access operator is a period between the name of a structure variable and the structure member we want to access .

The following example demonstrates the use of structure :

#include <iostream>
#include <cstring>
 
using namespace std;
 
//  Declare a struct type  Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        //  Define the structure type  Books  The variable of  Book1
   Books Book2;        //  Define the structure type  Books  The variable of  Book2
 
   // Book1  detailed 
   strcpy( Book1.title, "C++  course ");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, " programing language ");
   Book1.book_id = 12345;
 
   // Book2  detailed 
   strcpy( Book2.title, "CSS  course ");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, " The front-end technology ");
   Book2.book_id = 12346;
 
   //  Output  Book1  Information 
   cout << " The title of the first book  : " << Book1.title <<endl;
   cout << " The first book was written by  : " << Book1.author <<endl;
   cout << " First book category  : " << Book1.subject <<endl;
   cout << " The first book  ID : " << Book1.book_id <<endl;
 
   //  Output  Book2  Information 
   cout << " The title of the second book  : " << Book2.title <<endl;
   cout << " The author of the second book  : " << Book2.author <<endl;
   cout << " The second category of books  : " << Book2.subject <<endl;
   cout << " The second book  ID : " << Book2.book_id <<endl;
 
   return 0;
}

The structure type is defined in the example Books And its two variables Book1 and Book2. When the above code is compiled and executed , It will produce the following results :

Structure as a function parameter

You can take the structure as a function parameter , Parameters are passed in a similar way to other types of variables or pointers . You can access structure variables in the same way as in the example above :

#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books book );
 
//  Declare a struct type  Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        //  Define the structure type  Books  The variable of  Book1
   Books Book2;        //  Define the structure type  Books  The variable of  Book2
 
    // Book1  detailed 
   strcpy( Book1.title, "C++  course ");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, " programing language ");
   Book1.book_id = 12345;
 
   // Book2  detailed 
   strcpy( Book2.title, "CSS  course ");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, " The front-end technology ");
   Book2.book_id = 12346;
 
   //  Output  Book1  Information 
   printBook( Book1 );
 
   //  Output  Book2  Information 
   printBook( Book2 );
 
   return 0;
}
void printBook( struct Books book )
{
   cout << " The title of the book  : " << book.title <<endl;
   cout << " The author of the book  : " << book.author <<endl;
   cout << " Book category  : " << book.subject <<endl;
   cout << " book  ID : " << book.book_id <<endl;
}

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

Pointer to structure

You can define pointers to structures , This is similar to defining pointers to other types of variables , 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 , Please put & Operator before structure name , As shown below :

struct_pointer = &Book1;

To access members of a structure using pointers to that structure , You must use -> Operator , As shown below :

struct_pointer->title;

Let's use structure pointers to override the above example , This 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;        //  Define the structure type  Books  The variable of  Book1
   Books Book2;        //  Define the structure type  Books  The variable of  Book2
 
    // Book1  detailed 
   strcpy( Book1.title, "C++  course ");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, " programing language ");
   Book1.book_id = 12345;
 
   // Book2  detailed 
   strcpy( Book2.title, "CSS  course ");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, " The front-end technology ");
   Book2.book_id = 12346;
 
   //  By transmission  Book1  The address to output  Book1  Information 
   printBook( &Book1 );
 
   //  By transmission  Book2  The address to output  Book2  Information 
   printBook( &Book2 );
 
   return 0;
}
//  The function takes the structure pointer as an argument 
void printBook( struct Books *book )
{
   cout << " The title of the book   : " << book->title <<endl;
   cout << " The author of the book  : " << book->author <<endl;
   cout << " Book category  : " << book->subject <<endl;
   cout << " book  ID : " << book->book_id <<endl;
}

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

typedef keyword

Here's a simpler way to define structure , You can take one for the created type " Alias ". for example :

Now? , You can use  Books  To define  Books  Variable of type , Instead of using struct keyword . Here is an example :

Books Book1, Book2;

You can use  typedef  Keywords to define unstructured types , As shown below :

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

x, y and z They all point to long integers long int The pointer to .

 

版权声明
本文为[Three ah three water]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224224954397H.html

Scroll to Top