1.定义结构体数组

和定义结构体变量类似,定义结构体数组时只需声明其为数组即可。如:

struct Student{
int num;
char name[20];
char sex[5];
int age;
float score;
char addr[30];
};
Student stu[3]; //定义Student类型的数组stu

2.结构体数组的应用举例

题目:对候选人的票的统计程序。

设有3个候选人,最终只能有一个当选为领导。今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,要求最后能输出这3个候选人的的票结果。

#include<iostream>
using namespace std;
struct Person{
    char name[20];                       //姓名
    int count;                           //票数计数器
};
int main(){
    Person leader[3]={"Tom",0,"Neo",0,"Marry",0};
                                        //定义Person类型的数组,内容为3个候选人的姓名和票数
    int i,j,k=0;
    bool tag;
    cout<<"please input the name of the leader : Tom Neo Marry\n\n";
    char leadername[20];                //该数组为每次输入的候选人的名字
    for(i=0;i<10;i++){                   //循环输入这10个人选的候选人的名字
        cout<<"input name "<<i+1<<" :";
        cin>>leadername;
        tag=1;
        for(j=0;j<3;j++){
            if(strcmp(leadername,leader[j].name)==0){
                leader[j].count++;
                tag=0;
            }
        }
        if(tag==1)k++;
    }
    cout<<endl;
    for(i=0;i<3;i++){
       cout<<leader[i].name<<":"<<leader[i].count<<endl;    
    }  
    cout<<"Abandoned tickets:"<<k<<endl;
    return 0;
}

当然,如果不使用结构体也可以解决这个问题:

#include<iostream>
#include<string>
using namespace std;
int main(){
char *name[3]={"Tom","Neo","Marry"};
int count[3]={0,0,0};
int i,j,k=0;
bool tag=1;
cout<<"please input the name of the leader : Tom Neo Marry\n\n";
char leadername[20];
for(i=0;i<10;i++){
cout<<"input name "<<i+1<<" :";
cin>>leadername;
for(j=0;j<3;j++){
if(strcmp(leadername,name[j])==0){
count[j]++;
tag=0;
}
}
if(tag==1)k++;
tag=1;
}
cout<<endl;
for(i=0;i<3;i++){
cout<<name[i]<<":"<<count[i]<<endl;
}
cout<<"Abandoned tickets:"<<k<<endl;
return 0;
}

或者

#include<iostream>
#include<string>
using namespace std;
int main(){
string name[3]={"Tom","Neo","Marry"};
int count[3]={0,0,0};
int i,j,k=0;
bool tag=1;
cout<<"please input the name of the leader : Tom Neo Marry\n\n";
string leadername;
for(i=0;i<10;i++){
cout<<"input name "<<i+1<<" :";
cin>>leadername;
for(j=0;j<3;j++){
if(leadername==name[j]){
count[j]++;
tag=0;
}
}
if(tag==1)k++;
tag=1;
}
cout<<endl;
for(i=0;i<3;i++){
cout<<name[i]<<":"<<count[i]<<endl;
}
cout<<"Abandoned tickets:"<<k<<endl;
return 0;
}

但是,相比较使用结构体的方法,我们对于候选人和票数的关系,更加直观,联系更加明显。

结构体数组(C++)的更多相关文章

  1. C#调用C/C++动态库 封送结构体,结构体数组

    一. 结构体的传递 #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数 typedef struct { int ...

  2. 【C语言入门教程】7.2 结构体数组的定义和引用

    7.2 结构体数组的定义和引用 当需要使用大量的结构体变量时,可使用结构体定义数组,该数组包含与结构体相同的数据结构所组成的连续存储空间.如下例所示: struct student stu_a[50] ...

  3. Delphi结构体数组指针的问题

    //这段代码在Delphi 2007和delphi 7下是可以执行的,所以正确使用结构体数组和指针应该是这样的,已验证 unit Unit1; interface uses Windows, Mess ...

  4. C语言中的结构体,结构体数组

    C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...

  5. c语言学习之基础知识点介绍(十七):写入读取结构体、数组、结构体数组

    一.结构体的写入和读取 //写入结构体 FILE *fp = fopen("/Users/ios/Desktop/1.data", "w"); if (fp) ...

  6. c语言结构体数组定义的三种方式

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; int dangdang;//成员名可以和类名同名 }ddd[];/ ...

  7. C#调用C++DLL传递结构体数组的终极解决方案

    在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...

  8. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/art ...

  9. C#调用C/C++动态库 封送结构体,结构体数组

    因为实验室图像处理的算法都是在OpenCV下写的,还有就是导航的算法也是用C++写的,然后界面部分要求在C#下写,所以不管是Socket通信,还是调用OpenCV的DLL模块,都设计到了C#和C++数 ...

随机推荐

  1. python image模块

    Image 模块 Image 模块提供了同名的类用来表示PIL的图像.Image模块还提供了许多工厂(factory)函数,包块从文件加载图像的函数,以及创建新图像的函数.    例子  下面的脚本加 ...

  2. Eclipse导入git上的maven web项目 部署

    1 Eclipse中导入Git的maven项目 方法1: (1)首先当然是拉代码. 在Eclipse里面有个Git Repositories Exploring.就是Git仓库,clone a git ...

  3. asp.net中时间差的问题

    asp.net中时间差的问题 在asp中我们可以用datediff来处理,时间的差,相当的不错,可是在asp.net中C#语言中却没有.可是ASP.net给我们提供了一个TimeSpan,我们可以用它 ...

  4. ServletInvocableHandlerMethod:167 - Error resolving argument

    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(R ...

  5. Android开发之应用程序窗体显示状态操作(requestWindowFeature()的应用)

    转自:http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html 我们在开发程序是经常会需要软件全屏显示.自定义标题(使用按钮等控件)和其 ...

  6. BZOJ_1221_ [HNOI2001]_软件开发(最小费用流,网络流24题#10)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1221 n天,每天需要r个毛巾,用完以后可以洗,要么花fa洗a天,要么花fb洗b天,毛巾不够了可 ...

  7. 【转】CString类型互转 int

    CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int  将字 ...

  8. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents

    I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry t ...

  9. Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯

    WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...

  10. POJ 2449

    #include<queue> #include<cstdio> #include<string> #include<cstring> #include ...