C++ データ構造
C/C++ 配列を使用すると、同じ種類の複数のデータ項目を組み合わせた変数を定義できますが、構造 は、異なる種類のデータ項目を組み合わせることができる別のユーザー定義データ型です。
構造体はレコードを表すために使用されます。たとえば、図書館で自分の本を追跡したいとします。各本について次の属性を追跡することをお勧めします −
- タイトル
- 著者
- 件名
- 書籍 ID
構造の定義
構造体を定義するには、struct ステートメントを使用する必要があります。 struct ステートメントは、プログラム用に複数のメンバーを持つ新しいデータ型を定義します。 struct文のフォーマットはこれです −
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
構造タグ オプションであり、各メンバー定義は int i などの通常の変数定義です。またはフロート f;またはその他の有効な変数定義。構造体の定義の最後、最後のセミコロンの前に、1 つ以上の構造体変数を指定できますが、これはオプションです。 Book 構造を宣言する方法は次のとおりです −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
構造体メンバーへのアクセス
構造体のメンバーにアクセスするには、メンバー アクセス演算子 (.) を使用します。 .メンバー アクセス演算子は、構造体変数名とアクセスしたい構造体メンバーの間のピリオドとしてコーディングされます。 struct を使用します 構造体型の変数を定義するキーワード。以下は、構造体の使用法を説明する例です-
ライブデモ
#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; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
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;
// Print Book2 info
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;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
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
関数の引数としての構造体
他の変数やポインターを渡すのと非常によく似た方法で、構造体を関数の引数として渡すことができます。上記の例でアクセスしたのと同様の方法で構造変数にアクセスします-
ライブデモ
#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; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
printBook( Book1 );
// Print Book2 info
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;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
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
構造体へのポインタ
次のように他の変数へのポインターを定義するのと非常によく似た方法で構造体へのポインターを定義できます-
struct Books *struct_pointer;
これで、構造体変数のアドレスを上記で定義したポインター変数に格納できます。構造変数のアドレスを見つけるには、次のように構造の名前の前に &演算子を配置します −
struct_pointer = &Book1;
その構造体へのポインターを使用して構造体のメンバーにアクセスするには、次のように -> 演算子を使用する必要があります −
struct_pointer->title;
上記の例を構造ポインタを使用して書き直してみましょう。これが概念を理解しやすいことを願っています −
ライブデモ
#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; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info, passing address of structure
printBook( &Book1 );
// Print Book1 info, passing address of structure
printBook( &Book2 );
return 0;
}
// This function accept pointer to structure as parameter.
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;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
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 キーワード
構造体を定義する簡単な方法があります。または、作成した型を「エイリアス」することもできます。たとえば-
typedef struct {
char title[50];
char author[50];
char subject[100];
int book_id;
} Books;
本を使えるようになりました Books の変数を直接定義する struct キーワードを使用せずに入力します。以下は例です-
Books Book1, Book2;
typedef を使用できます 非構造体のキーワードと次のキーワード-
typedef long int *pint32; pint32 x, y, z;
x、y、z はすべて long int へのポインターです。
C言語