前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《挑战30天C++入门极限》新手入门:C/C++中的结构体

《挑战30天C++入门极限》新手入门:C/C++中的结构体

作者头像
landv
发布2019-07-15 16:56:16
2550
发布2019-07-15 16:56:16
举报
文章被收录于专栏:landv
代码语言:javascript
复制






 

新手入门:C/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<<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[i]);  
    }  
    for(int i=0;i<num;i++)  
    {  

        print_score(&a[i]);  
    }  
    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[i]);  
    }  
    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[i]=get_score();  
    }  
    cin.get();  
    for(int i=0;i<num;i++)  
    {  

        print_score(a[i]);  
    }  
    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[i]);  
    }  
    cin.get();  
    for(int i=0;i<num;i++)  
    {  

        print_score(a[i]);  
    }  
    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[i]=get_score(); 
} 




  而例程2中由于是通过内存地址直接操作,所以完全没有这一过程,提高了效率。
for(int i=0;i<num;i++) 
{ 
get_score(a[i]); 
} 

  函数也是可以返回结构体应用的,例子如下:

#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就是一个指向指针的引用,它也可以看做是指针别名,总之使用引用要特别注意它的特性,它的操作是和普通指针一样的,在函数中对全局指针的引用操作要十分小心,避免破坏全局指针!

 




 




 











本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档