zoukankan      html  css  js  c++  java
  • 数据结构(Data structures)(一):结构体

    一个结构体(data structure)就是 一组数据元素被集合在一起,放在一个名字下面。这些数据元素,被称为 成员(members),可以有不同的类型(type)和长度(length)。定义数据结构的语法如下:

        struct type_name{

         member_type1 member_name1;

         member_type2 member_name2;

         member_type3 member_name3;

        ...

        ...

        } object_names;

    type_name 是这个结构体的名字,object_names 可以是一些对象的有效标识符,这些对象拥有上面定义的结构类型。在符号 {} 之中,有一系列的数据成员,每一个都指定有一个类型和一个效标识符作为它的名字。

    例如:

        struct product {

        int weight;

        double price;

        };      //注意这个分号 ";" ,何时有,何时没有

        product apple;

        product banana, melon;

    这里声明了一个结构体 :product 。并且它定义了两个成员:weight 和 price ,每一个都有一个不同的基本类型。

    这个声明了创造了一个新的类型( product ),然后用它定义了该类型的三个对象 objects(variables):apple,banana,melon。

    要注意,类型 product 一旦被定义,它就可以像使用其他数据类型如:int,float,double 一样进行使用。

    在结构定义的结尾处有可选项 object_names ,它的作用是可以直接定义该结构类型的对象。例如我们可以在定义数据结构类型的同时,声明结构对象 apple,banana,melon :

        struct product {

        int weight;

        double price;

        }apple, banana, melon;

    在这里,object_names 需要被指定,而类型名(product)成了可选项:struct 需要一个 type_name 或至少一个 object_names 里的名字,但不一定都需要。

    清楚的区分结构类型名(product)和该结构类型的对象(apple, banana, melon)是很重要的。可以从一个结构类型中声明出(实例化)多个对象(如 apple, banana, melon) 。

    一旦这三个确定的结构类型对象被声明后(apple, banana, melon),它的成员(members)可以被直接访问。这个语法是在object name 和 member name 之间加一个逗号 ”.“。例如,我们可以像使用一般的标准变量一样对这些元素进行操作:

     

        apple.weight;

        apple.price;

        banana.weight;

        banana.price;

        melon.weight;

        melon.price;

    它们和各自的数据类型相对应,apple.weight, banana.weight, 和 melon.weight 是 int 类型;apple.price, banana.price 和 melon.price 是 double 类型。

    下面给出一个实际的例子:

    #include <iostream>
    #include <string>
    #include <sstring>
    using namespace std;
    struct movies_t
    {
    string title;
    int years
    } 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: "; printmovie (mine); cout << "And yours is: "; printmovie (yours);

    return 0; }
    void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ") "; }
    Enter title: Alien
    Enter year: 1979
    
    My favorite movie is:
    2001 A Space Odyssey (1968)
    And yours is:
    Alien (1979)
     

    这个例子中我们可以看到如何像使用普通变量一样使用一个对象的成员(members)。例如,yours.year 是一个整型数据 int,而 mine.title 是 一个string 类型的有效变量。

    注意这里 mine 和 yours 也是变量,他们是movies_t 类型的变量,被传递给函数printmovie()。因此,数据结构的重要优点之一就是我们既可以单独引用它的元素,也可以引用整个结构数据块。

    结构经常被用来建立数据库,特别是当我们考虑结构数组的时候:

    #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 << " You have entered these movies: "; for(n=0; n<3; n++) printmovie (films[n]);
    return 0; }
    void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ") "; }
    Enter title: Blade Runner
    Enter year: 1982
    Enter title: The Matrix
    Enter year: 1999
    Enter title: Taxi Driver
    Enter year: 1976
     
    You have entered these movies:
    Blade Runner (1982)
    The Matrix (1999)
    Taxi Driver (1976)



  • 相关阅读:
    web移动端开发经验总结
    《前端JavaScript面试技巧》笔记一
    《SEO在网页制作中的应用》视频笔记
    web前端开发笔记(2)
    OAuth2.0理解和用法
    基于hdfs文件创建hive表
    kafka 配置事项
    centos7时间同步
    lambda架构
    hbase hadoop版本
  • 原文地址:https://www.cnblogs.com/guozqzzu/p/3624630.html
Copyright © 2011-2022 走看看