C++ structure array in detail

  • 2020-04-02 01:49:02
  • OfStack

1. Define an array of structs

Similar to defining a struct variable, you define an array of structs by declaring it an array. Such as:


struct Student{
     int num;
     char name[20];
     char sex[5];
     int age;
     float score;
     char addr[30];
};
Student stu[3]; //Define an array of Student type stu

2. Examples of structural array application

Title: procedure for counting the votes of candidates.

With three candidates, only one can be elected as the leader. Today, there are 10 people to vote. Enter the names of the candidates from the keyboard and ask for the results of the three candidates.


#include<iostream>
using namespace std;
struct Person{
    char name[20];                       //The name
    int count;                           //Ticket counter
};
int main(){
    Person leader[3]={"Tom",0,"Neo",0,"Marry",0};
                                        //Defines an array of type Person with the names and votes of three candidates
    int i,j,k=0;
    bool tag;
    cout<<"please input the name of the leader : Tom Neo Marrynn";
    char leadername[20];                //The array is the name of the candidate entered each time
    for(i=0;i<10;i++){                   //Loop in the names of the 10 candidates
        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;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/2013101610041913.jpg ">

Of course, you can solve this problem without using structures:


#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 Marrynn";
 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;
}

or

#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 Marrynn";
 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;
}

However, compared with the structured approach, we have a more intuitive and obvious relationship between candidates and votes.


Related articles: