zoukankan      html  css  js  c++  java
  • (转) 新手入门:C/C++中的结构体

    本文转载于 http://pcedu.pconline.com.cn/empolder/gj/c/0503/567930_all.html#content_page_1

    所有程序经过本人验证,部分程序经过修改: 验证平台 w530 ,ubuntu 12.10_x64, ecplise 4.3(with CDT)

    1. 定义:构体就是一个可以包含不同数据类型的一个结构,它是一种可以自己定义的数据类型,

    2. 与数组不同的两点

    • 结构体可以在一个结构中声明不同的数据类型,
    • 相同结构的结构体变量是可以相互赋值的,(而数组是做不到的因为数组是单一数据类型的数据集合,它本身不是数据类型(而结构体是),数组名称 是常量指针,所以不可以做为左值进行运算,所以数组之间就不能通过数组名称相互复制了,即使数据类型和数组大小完全相同。)

    3. 定义结构体使用struct修饰符

    struct test { float a; int b; }; 

    上面的代码就定义了一个名为test的结构体,它的数据类型就是test,它包含两个成员a和b,成员a的数据类型为浮点型,成员b的数据类型为整型

    4. 如何定义结构体变量:和定义普通变量的方法一样

    例如

    test pn1;

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

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

    5. 如何定义结构体指针:结构指针通过->符号来访问成员

    #include <iostream> #include <string>
    using namespace std; struct test//定义一个名为test的结构体
    { int a;//定义结构体成员a
        int b;//定义结构体成员b
    }; int main() //在新的c++ 标准中,要使用 int main() 和 return 0
    { 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;

    return 0; }

    显示结果:

    10|3
    10|3
    10|3
    99|3
    99|3
    • 结构体变量是如何作为函数参数进行传递
    #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<<endl; } int 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[i]); } for(int i=0;i<num;i++) { print_score(&a[i]); } return 0; }

    显示结果

    marry|88.5 jarck|98.5 marry|88.5 jarck|98.5

    注意: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; } int 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[i]); } return 0; }

    显示结果

    marry|88.5 jarck|98.5

    6. 上面我们说明了易用引用对结构体进行操作的优势,下面我们重点对比两个例程,进一部分析关于效率的问题。

    例程1 例程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; } test get_score() { test pn; cin>>pn.name>>pn.socre; return pn; } int main() { test a[2]; int num = sizeof(a)/sizeof(test); for(int i=0;i<num;i++) { a[i]=get_score(); } for(int i=0;i<num;i++) { print_score(a[i]); } return 0; }
    #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; } int main() { test a[2]; int num = sizeof(a)/sizeof(test); for(int i=0;i<num;i++) { get_score(a[i]); } for(int i=0;i<num;i++) { print_score(a[i]); } return 0; }
  • 相关阅读:
    SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)
    SQL Server 多实例下的复制
    SQL Server 批量主分区备份(Multiple Jobs)
    SQL Server 批量完整备份
    SQL Server 游标运用:鼠标轨迹字符串分割
    SQL Server 批量主分区备份(One Job)
    SQL Server 即时文件初始化
    (原+转)Ubuntu中设置程序可用的cpu核心
    (原)Non-local Neural Networks
    (原)softmax loss特征为何径向放射状分布(直观理解,非公式推导。。。)
  • 原文地址:https://www.cnblogs.com/assassin/p/3520023.html
Copyright © 2011-2022 走看看