산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C++ 데이터 구조

C/C++ 배열을 사용하면 같은 종류의 여러 데이터 항목을 결합하는 변수를 정의할 수 있지만 구조 다른 종류의 데이터 항목을 결합할 수 있는 또 다른 사용자 정의 데이터 유형입니다.

구조는 레코드를 나타내는 데 사용됩니다. 도서관에서 책을 추적하려고 한다고 가정합니다. 각 책에 대한 다음 속성을 추적할 수 있습니다. −

구조 정의

구조를 정의하려면 struct 문을 사용해야 합니다. struct 문은 프로그램에 대해 둘 이상의 멤버가 있는 새 데이터 유형을 정의합니다. struct 문의 형식은 다음과 같습니다. -

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

구조 태그 선택적이며 각 멤버 정의는 int i와 같은 일반 변수 정의입니다. 또는 float f; 또는 다른 유효한 변수 정의. 구조 정의 끝에서 마지막 세미콜론 앞에 하나 이상의 구조 변수를 지정할 수 있지만 선택 사항입니다. Book 구조를 선언하는 방법은 다음과 같습니다. -

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

구조 구성원 액세스

구조의 모든 멤버에 액세스하려면 멤버 액세스 연산자(.)를 사용합니다. . 멤버 액세스 연산자는 구조 변수 이름과 액세스하려는 구조 멤버 사이의 마침표로 코딩됩니다. 구조체를 사용합니다. 구조체 유형의 변수를 정의하는 키워드입니다. 다음은 구조체의 사용법을 설명하는 예입니다 -

라이브 데모
#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;

이제 도서를 사용할 수 있습니다. Book의 변수를 직접 정의 struct 키워드를 사용하지 않고 입력합니다. 다음은 예입니다 -

Books Book1, Book2;

typedef를 사용할 수 있습니다. 비구조체에 대한 키워드 및 다음 -

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

x, y 및 z는 모두 long int에 대한 포인터입니다.


C 언어

  1. C++ 데이터 유형
  2. C++ 연산자
  3. C++ 주석
  4. C++ Char 데이터 유형(예제 포함)
  5. 차세대 자기 데이터 저장 기술
  6. 자바 - 데이터 구조
  7. C - 구조
  8. C - 형식 정의
  9. C++ 데이터 구조
  10. C++의 데이터 추상화