zoukankan      html  css  js  c++  java
  • C结构体

    什么是结构体?
      简单的来说,结构体就是个能够包含不同数据类型的一个结构,他是一种能够自己定义的数据类型,他的特点和数组主要有两点不同,首先结构体能够在一个结构中声明不同的数据类型,第二相同结构的结构体变量是能够相互赋值的,而数组是做不到的,因为数组是单一数据类型的数据集合,他本身不是数据类型(而结构体是),数组名称是常量指针,所以不能够做为左值进行运算,所以数组之间就不能通过数组名称相互复制了,即使数据类型和数组大小完全相同。

      定义结构体使用struct修饰符,例如:

    struct test
    {
    float a;
    int b;
    };
       上面的代码就定义了一个名为test的结构体,他的数据类型就是test,他包含两个成员a和b,成员a的数据类型为浮点型,成员b的数据类型为整型。

      由于结构体本身就是自定义的数据类型,定义结构体变量的方法和定义普通变量的方法相同。

    test pn1;
       这样就定义了一test结构体数据类型的结构体变量pn1,结构体成员的访问通过点操作符进行,pn1.a=10 就对结构体变量pn1的成员a进行了赋值操作。

      注意:结构体生命的时候本身不占用任何内存空间,只有当您用您定义的结构体类型定义结构体变量的时候电脑才会分配内存。

      结构体,同样是能够定义指针的,那么结构体指针就叫做结构指针。

      结构指针通过->符号来访问成员,下面我们就以上所说的看一个完整的例子:

    #include <iostream>
    #include <string>
    using namespace std;

    struct test//定义一个名为test的结构体
    {
    int a;//定义结构体成员a
    int b;//定义结构体成员b
    };

    void main()
    {
    test pn1;//定义结构体变量pn1
    test pn2;//定义结构体变量pn2

    pn2.a=10;//通过成员操作符.给结构体变量pn2中的成员a赋值
    pn2.b=3;//通过成员操作符.给结构体变量pn2中的成员b赋值

    pn1=pn2;//把pn2中任何的成员值复制给具备相同结构的结构体变量pn1
    cout<<pn1.a<<"|"<<pn1.b<<endl;
    cout<<pn2.a<<"|"<<pn2.b<<endl;

    test *point;//定义结构指针

    point=&pn2;//指针指向结构体变量pn2的内存地址
    cout<<pn2.a<<"|"<<pn2.b<<endl;
    point->a=99;//通过结构指针修改结构体变量pn2成员a的值
    cout<<pn2.a<<"|"<<pn2.b<<endl;
    cout<<point->a<<"|"<<point->b<< ;endl;
    cin.get();
    }
       总之,结构体能够描述数组不能够清楚描述的结构,他具备数组所不具备的一些功能特性。
       下面我们来看一下,结构体变量是如何作为函数参数进行传递的。

    #include <iostream>
    #include <string>
    using namespace std;

    struct test
    {
    char name[10];
    float socre;
    };

    void print_score(test pn)//以结构变量进行传递
    {
    cout<<pn.name<<"|"<<pn.socre<<endl;
    }

    void print_score(test *pn)//一结构指针作为形参
    {
    cout<<pn->name<<"|"<<pn->socre<&l t;endl;
    }

    void main()
    {
    test a[2]={{"marry",88.5},{"jarck",98.5}};
    int num = sizeof(a)/sizeof(test);
    for(int i=0;i<num;i )
    {
    print_score(a);
    }
    for(int i=0;i<num;i )
    {
    print_score(&a);
    }
    cin.get();
    }
       void print_score(test *pn)的效率是要高过void print_score(test pn)的,因为直接内存操作避免了栈空间开辟结构变量空间需求,节省内存。

      下面我们再说一下,传递结构引用的例子。

      利用引用传递的好处很多,他的效率和指针相差无几,但引用的操作方式和值传递几乎相同,种种优势都说明善用引用能够做到程式的易读和易操作,他的优势尤其在结构和大的时候,避免传递结构变量很大的值,节省内存,提高效率。

    #include <iostream>
    #include <string>
    using namespace std;

    struct test
    {
    char name[10];
    float socre;
    };

    void print_score(test &pn)//以结构变量进行传递
    {
    cout<<pn.name<<"|"<<pn.socre<<endl;
    }

    void main()
    {
    test a[2]={{"marry",88.5},{"jarck",98.5}};
    int num = sizeof(a)/sizeof(test);
    for(int i=0;i<num;i )
    {
    print_score(a);
    }
    cin.get();
    }
       上面我们说明了易用引用对结构体进行操作的优势,下面我们重点对比两个例程,进一部分析关于效率的问题。

    //-------------------------------------例程1---------------------------------

    #include <iostream>
    #include <string>
    using namespace std;

    struct test
    {
    char name[10];
    float socre;
    };

    void print_score(test &pn)
    {
    cout<<pn.name<<"|"<<pn.socre<<endl;
    }

    test get_score()
    {
    test pn;
    cin>>pn.name>>pn.socre;
    return pn;
    }
    void main()
    {
    test a[2];
    int num = sizeof(a)/sizeof(test);
    for(int i=0;i<num;i )
    {
    a=get_score();
    }
    cin.get();
    for(int i=0;i<num;i )
    {
    print_score(a);
    }
    cin.get();
    }

    //-------------------------------------例程2---------------------------------

    #include <iostream>
    #include <string>

    using namespace std;

    struct test
    {
    char name[10];
    float socre;
    };

    void print_score(test &pn)
    {
    cout<<pn.name<<"|"<<pn.socre<<endl;
    }

    void get_score(test &pn)
    {
    cin>>pn.name>>pn.socre;
    }
    void main()
    {
    test a[2];
    int num = sizeof(a)/sizeof(test);
    for(int i=0;i<num;i )
    {
    get_score(a);
    }
    cin.get();
    for(int i=0;i<num;i )
    {
    print_score(a);
    }
    cin.get();
    }
       例程2的效率要远高过例程1的原因主要有以下两处:


      第一:

      例程1中的

    test get_score()
    {
    test pn;
    cin>>pn.name>>pn.socre;
    return pn;
    }
       调用的时候在内部要在栈空间开辟一个名为pn的结构体变量,程式pn的时候又再次在栈内存空间内自动生成了一个临时结构体变量temp,在前面的教程中我们已说过,他是个copy,而例程2中的: void get_score(test &pn)
    {
    cin>>pn.name>>pn.socre;
    }
       却没有这一过程,不开辟任何新的内存空间,也没有任何临时变量的生成。

      第二:

      例程1在mian()中,必须对返回的结构体变量进行一次结构体变量和结构体变量直接的相互赋值操作。 for(int i=0;i<num;i )
    {
    a=get_score();
    }
       而例程2中由于是通过内存地址直接操作,所以完全没有这一过程,提高了效率。 for(int i=0;i<num;i )
    {
    get_score(a);
    }
       函数也是能够返回结构体应用的,例子如下:

    #include <iostream>
    #include <string>
    using namespace std;

    struct test
    {
    char name[10];
    float socre;
    };

    test a;


    test &get_score(test &pn)
    {
    cin>>pn.name>>pn.socre;
    return pn;
    }

    void print_score(test &pn)
    {
    cout<<pn.name<<"|"<<pn.socre<<endl;
    }

    void main()
    {
    test &sp=get_score(a);
    cin.get();
    cout<<sp.name<<"|"<<sp.socre;
    cin.get();
    }
       调用get_score(a);结束并返回的时候,函数内部没有临时变量的产生,返回直接吧全局结构变量a的内存地址赋予结构引用sp

      最后提一下指针的引用

      定义指针的引用方法如下:

    void main()
    {
    int a=0;
    int b=10;
    int *p1=&a;
    int *p2=&b;
    int *&pn=p1;
    cout <<pn<<"|"<<*pn<<endl;
    pn=p2;
    cout <<pn<<"|"<<*pn<<endl;
    cin.get();
    }
       pn就是个指向指针的引用,他也能够看做是指针别名,总之使用引用要特别注意他的特性,他的操作是和普通指针相同的,在函数中对全局指针的引用操作要十分小心,避免破坏全局指针!

  • 相关阅读:
    7.21 高博教育 数组 内存
    【基础扎实】Python操作Excel三模块
    PAT 甲级 1012 The Best Rank
    PAT 甲级 1011  World Cup Betting
    PAT 甲级 1010 Radix
    链式线性表——实验及提升训练
    循环程序设计能力自测
    链表应用能力自测
    PAT 甲级 1009 Product of Polynomials
    1008 Elevator (20分)
  • 原文地址:https://www.cnblogs.com/timssd/p/4078086.html
Copyright © 2011-2022 走看看