LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 255|回复: 0

数据结构

[复制链接]
发表于 2024-2-20 23:31:53 | 显示全部楼层 |阅读模式
数据结构是一组数据元素,在一个名称下组合在一起。这些数据元素(称为成员)可以具有不同的类型和不同的长度。可以使用以下语法在 C++ 中声明数据结构:


其中 是结构类型的名称,可以是具有此结构类型的对象的一组有效标识符。在大括号内,有一个包含数据成员的列表,每个数据成员都指定了一个类型和一个有效的标识符作为其名称。

例如: struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
type_nameobject_name{}

struct product {
  int weight;
  double price;
} ;

product apple;
product banana, melon;

这声明了一个名为 的结构类型,并定义它有两个成员:和 ,每个成员都是不同的基本类型。此声明创建一个新类型 (),然后用于声明此类型的三个对象(变量):、 和 。请注意,一旦声明,它的使用方式与任何其他类型的类型一样。

在定义的末尾,在结束分号 () 之前,可选字段可用于直接声明结构类型的对象。例如,结构对象 、 和 可以在定义数据结构类型时声明:productweightpriceproductapplebananamelonproductstruct;object_namesapplebananamelon

struct product {
  int weight;
  double price;
} apple, banana, melon;

在本例中,如果指定了 ,则类型名称 () 变为可选:需要 中的一个或至少一个名称,但不一定同时需要两个名称。

重要的是要清楚地区分什么是结构类型名称 () 和什么是这种类型的对象 (, , 和 )。许多对象(如 、 和 )可以从单个结构类型 () 声明。

一旦声明了已确定结构类型的三个对象(、、和),就可以直接访问其成员。其语法只是在对象名称和成员名称之间插入一个点 ()。例如,我们可以对这些元素中的任何一个进行操作,就好像它们是各自类型的标准变量一样:object_namesproductstructtype_nameobject_namesproductapplebananamelonapplebananamelonproductapplebananamelon.

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

它们中的每一个都具有与它们所引用的成员相对应的数据类型:、 和 的类型为 ,而 、 和 的类型为 。

下面是一个实际使用结构类型的真实示例:apple.weightbanana.weightmelon.weightintapple.pricebanana.pricemelon.pricedouble

// 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";
}
Enter title: Alien
Enter year: 1979

My favorite movie is:
2001 A Space Odyssey (1968)
And yours is:
Alien (1979)

该示例演示对象的成员如何充当常规变量。例如,成员是 类型的有效变量 ,并且是 类型的有效变量。

但是对象 和 也是具有类型 (的 类型) 的变量。例如,两者都被传递给函数,就好像它们是简单的变量一样。因此,数据结构的特征之一是能够单独引用它们的成员或整个结构。在这两种情况下,都使用相同的标识符:结构的名称。

因为结构是类型,所以它们也可以用作数组的类型来构造它们的表或数据库:yours.yearintmine.titlestringmineyoursmovies_tprintmovie

// 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";
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表