C + + Tutorials (15: Data structures)

Source: Internet
Author: User
———————————————————————— This series of tutorials for the translation of C + + official tutorials, click on the English version of the original, the level is limited, translation is not in place please understand. ———————————————————————— Data Structure Body

A data structure is a collection of sets of elements under a name. These data elements, called members, can have different types and lengths. Data structures can be declared in C + + using the following syntax:

struct Type_name {
member_type1 member_name1;
Member_type2 member_name2;
Member_type3 Member_name3;
} Object_names;

Here type_name is the name of the structure type,
In Type_name is the name of the struct, object_name can be an identifier for the object of this struct type. In curly braces, there is a list of data members, each of which specifies a type and a valid identifier as its name.

For example:

struct Product {
  int weight;
  Double price;
} ;

Product Apple;
Product Banana, melon

This declares the product of a data structure type and defines two data elements inside: weight and price, the two types of data are not the same. This creates a new data type (product) and then declares three variables with this data type: Apple, Banana,melon. Notice how product is declared, and it is the same as the use of other variables.

After the end of the struct definition, you need to use the end semicolon (;), and the optional field object_names the object that can write the struct type directly. For example, the structure objects of apple, banana, and melon can be declared at the end of a data structure type definition:

struct Product {
  int weight;
  Double price;
} Apple, banana, melon;

In this case, object_names specifies that the type name is specific, and the type name (PRODIUCT) is optional: The structure requires at least one type_name, and object_names is optional.

Once the structure type of the three objects is declared (apple, banana, melon), its members can be accessed directly. The simple syntax is to insert a point (.) between the object name and the member name. For example, if these elements are in a standard variable of their own type, we can use any of these elements:

Apple.weight
apple.price
banana.weight
banana.price
melon.weight
melon.price

In these types of data elements, Apple.weight, Banana.weight, and melon.weight all point to the int type, while Apple.price, Banana.price, and Melon.price are double types.

The following is a concrete example of a struct:

Example about Structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} Mine, yours;

void Printmovie (movies_t movie);

int main ()
{
  string mystr;

  Mine.title = "2001 A space Odyssey";
  mine.year = 1968;

  cout << "Enter title:";
  Getline (cin,yours.title);
  cout << "Enter year:";
  Getline (CIN,MYSTR);
  StringStream (mystr) >> yours.year;

  cout << "My favorite movie is:\n";
  Printmovie (mine);
  cout << "and Yours is:\n";
  Printmovie (yours);
  return 0;
}

void Printmovie (movies_t movie)
{
  cout << movie.title;
  cout << "(" << movie.year << ") \ n";
}

The example shows how a member of an object acts as a regular variable. For example, member Yours.year is a valid variable of type int, mine.title is a valid variable of a string type.

But mine and your objects are variables of the same type (type movies_t). For example, both are passed to the function Printmovie as if they were simple variables. Thus, a characteristic of a data structure is that their members, either individually or in the entire structure, can act as a whole. In both cases, you can use the same identifier: the name of the struct body.

Because of the type structure of structs, they can also be used as types of arrays to build their tables or databases:

Array of Structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} Films [3];

void Printmovie (movies_t movie);

int main ()
{
  string mystr;
  int n;

  For (n=0 n<3; n++)
  {
    cout << "Enter title:";
    Getline (cin,films[n].title);
    cout << "Enter year:";
    Getline (CIN,MYSTR);
    StringStream (mystr) >> films[n].year;
  }

  cout << "\nyou have entered these movies:\n";
  For (n=0 n<3; n++)
    Printmovie (films[n));
  return 0;
}

void Printmovie (movies_t movie)
{
  cout << movie.title;
  cout << "(" << movie.year << ") \ n";
}
structure Body Pointer

Like other structures, structs can use pointers to point to themselves.

struct movies_t {
  string title;
  int year;
};

movies_t Amovie;
movies_t * Pmovie;

Here Amovie is a movies_t type structure, and Pmovie is a pointer to the movies_t type. So the above code can also be written as:
Pmovie = &amovie;

The value of the pointer Pmovie is the address of the struct Amovie.
Let's take a look at a mixture of pointers and structures, and introduce a new action symbol, Arrow action (->):

Pointers to Structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
};

int main ()
{
  string mystr;

  movies_t Amovie;
  movies_t * Pmovie;
  Pmovie = &amovie;

  cout << "Enter title:";
  Getline (CIN, pmovie->title);
  cout << "Enter year:";
  Getline (CIN, mystr);
  (stringstream) mystr >> pmovie->year;

  cout << "\nyou have entered:\n";
  cout << pmovie->title;
  cout << "(" << pmovie->year << ") \ n";

  return 0;
}

The arrow operator (->) is an operator and is a unique way of using pointers to access object members. This action can access the members of the object directly from its address. For example, in the above example:
Pmovie->title

This line is equivalent to:
(*pmovie). Title

Two expressions, Pmovie-> title and (*pmovie). Title is valid, all is to access the data structure pointer refers to the Pmovie member title. It is a completely different thing from the following definition:
*pmovie.title
This sentence is equivalent to:
* (Pmovie.title)

This would have meant a value to the address of the member title under the Pmovie of the struct (however, this is not possible because title is not a pointer type). The following table summarizes the possible combinations of operators for pointers and struct members:

Expression What is evaluated equivalent
A.b Member B of Object A
A->b Member B of the object pointed to by a (*a). b
*a.b Value pointed to by member B of object A * (A.B)
Nested structure Body

Structs can also be nested, i.e. one structure itself contains another structure:

struct movies_t {
  string title;
  int year;
};

struct friends_t {
  string name;
  string email;
  movies_t Favorite_movie;
} Charlie, Maria;

friends_t * Pfriends = &charlie;

After such a declaration, the following statements are valid:

Charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_ Movie.year

(The last two sentences express the same statement)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.