C++ data structures
C/C++ arrays allow you to define variables that can store data items of the same type, but structures are another user-defined data type available in C++ that allow you to store data items of different types.
A structure is used to represent a record. Suppose you want to track the activity of books in the library. You may need to track the following properties of each book:
- Title: Title
- Author : author
- Subject : category
- Book ID: ID of the book
define structure
In order to define a 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 a structure definition, before the final semicolon, you can specify one or more structure variables, this is optional. The following is to declare a structure type Books , the variable is book :
struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
access structure members
To access 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.
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, "Runoob");
strcpy( Book1.subject, "编程语言");
Book1.book_id = 12345;
// Book2 详述
strcpy( Book2.title, "CSS 教程");
strcpy( Book2.author, "Runoob");
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 the structure type Books and its two variables Book1 and Book2. When the above code is compiled and executed, it produces the following result:
第一本书标题: C++教程
第一本书作者:Runoob
第一本书类目:编程语言
第一本书 ID :12345
第二本书标题: CSS 教程
第二本书作者:Runoob
第二本书类目:前端技术
第二本书 ID :12346
Structs as Function Arguments
You can use a structure as a function parameter, and the method of passing parameters is similar to other types of variables or pointers. You can use 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, "Runoob");
strcpy( Book1.subject, "编程语言");
Book1.book_id = 12345;
// Book2 详述
strcpy( Book2.title, "CSS 教程");
strcpy( Book2.author, "Runoob");
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 produces the following result:
书标题: C++教程
书作者:Runoob
书类目:编程语言
书 ID :12345
书标题: CSS 教程
书作者:Runoob
书类目:前端技术
书 ID :12346
pointer to structure
You can define pointers to structures in a similar way to pointers to variables of other types, as follows:
structBooks*struct_pointer;
Now you can store the address of the structure variable in the above defined pointer variable. To find the address of a structure variable, put the & operator in front of the structure name, like this:
struct_pointer =&Book1;
In order to access a member of a struct using a pointer to that struct, you have to use the -> operator as follows:
struct_pointer->title;
Let us rewrite the above example using struct pointers, it will help you understand the concept of struct 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, "Runoob");
strcpy( Book1.subject, "编程语言");
Book1.book_id = 12345;
// Book2 详述
strcpy( Book2.title, "CSS 教程");
strcpy( Book2.author, "Runoob");
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 produces the following result:
书标题: C++教程
书作者:Runoob
书类目:编程语言
书 ID :12345
书标题: CSS 教程
书作者:Runoob
书类目:前端技术
书 ID :12346
typedef keyword
Here's a simpler way of defining structures where you can "alias" the type you create. For example:
typedefstructBooks
{
char title[50];
char author[50];
char subject[100];
int book_id;
}Books;
Now, you can use Books directly to define variables of type Books without using the struct keyword. Here is an example:
BooksBook1,Book2;
You can use the typedef keyword to define non-struct types as follows:
typedeflongint*pint32;
pint32 x, y, z;
x, y and z are all pointers to long int.