Explains the enumerated types in C++ and how to declare new types

  • 2020-05-05 11:31:34
  • OfStack

C++ enumeration type
can be defined as an enumeration (enumeration) type if there are only a few possible values for a variable. The so-called "enumeration" refers to enumerating the values of variables one by one, and the values of variables can only be listed within the range of values. Declare that an enumerated type begins with enum. For example:


  enum weekday{sun, mon, tue, wed, thu, fri, sat};

Above declare an enumeration type weekday, sun, mon... , sat and so on are called enumeration elements or enumeration constants. The value of a variable that represents this type can only be one of the seven values above. They are user-defined identifiers.

The general form for declaring an enumerated type is:


  enum  Enumerated type name { Enumerate the list of constants };

After declaring the enumerated type, you can use it to define variables. Such as:


  weekday workday,week_end;

Thus, workday and week_end are defined as variables of the enumerated type weekday.

In the C language, enumeration type names include the keyword enum, and the above definition can be written as:


  enum weekday workday,week_end;

enum is not allowed in C++, and enum is not generally used, but C is retained. According to the above declaration of the enumeration type weekday, the value of the enumeration variable can only be one of sun to sat. For example:


  workday=mon; week_end=sun;

That's right. You can also directly define enumeration variables, such as


  enum{sun, mon, tue, wed, thu, fri, sat} workday,week_end;

These identifiers do not automatically mean anything.

A few notes on enumerated types:
The enumeration element is treated as a constant, so it is called an enumeration constant.
Enumeration elements are constants that have values, and C++ compiles assign them to 0,1,2,3 in the order they were defined... . You can also specify the value of an enumeration element separately when declaring an enumeration type.
Enumeration values can be used for judgment comparisons.
An integer cannot be directly assigned to an enumeration variable.

There are several balls of red, yellow, blue, white and black in the pocket. Take 3 random balls out of your pocket at a time, ask for the possible ways to pick 3 different colored balls, and output each arrangement.


#include <iostream>
#include <iomanip>// I'm going to use it in the output setw Control characters 
using namespace std;
int main( )
{
  enum color {red,yellow,blue,white,black}; // Declare an enumerated type color
  color pri; // define color Variable of type pri
  int i,j,k,n=0,loop; //n Is the number of combinations of different colors 
  for (i=red;i<=black;i++) // when i Is a certain color 
   for (j=red;j<=black;j++) // when j Is a certain color 
     if (i!=j) // If the first two balls are different colors 
     {
      for (k=red;k<=black;k++) // Only if the first two balls are of different colors need to be checked 3 The color of the ball 
        if ((k!=i) && (k!=j)) //3 They're all different colors 
        {
         n=n+1;// The cumulative value n add 1
         cout<<setw(3)<<n; // Output current n Value, the field width is 3
         for (loop=1;loop<=3;loop++) // Successively for 3 One ball for handling 
         {
           switch (loop) //loop Is followed by 1,2,3
           {
             case 1: pri=color(i);break ; //color(i) Is a cast, so pri The value of i
             case 2: pri=color(j);break ; // make pri The value of j
             case 3: pri=color(k);break ; // make pri The value of k
             default :break ;
      }
      switch (pri)// judge pri Output the corresponding "color" 
      {
        case red: cout<<setw(8)<<"red"; break;
        case yellow: cout<<setw(8)<<"yellow";break;
        case blue:cout<<setw(8)<<"blue"; break ;
        case white:cout<<setw(8)<<"white"; break ;
        case black:cout<<setw(8)<<"black"; break ;
        default : break ;
      }
     }
     cout<<endl;
   }
  }
  cout<<"total:"<<n<<endl; // Output the number of qualified combinations 
  return 0;
}

The result is:


1 red yellow blue 2 red yellow white 3 red yellow black
 ┆ 
 ┆ 
 ┆ 
58 black white red
59 black white yellow
60 black white blue
total:60

Instead of enumerating constants, use constant 0 for "red" and 1 for "yellow"... Can also. But it's more intuitive to use enumeration variables, because enumeration elements use identifiers that make sense, and enumeration variables limit their value to several enumeration element ranges defined by   at the time of definition. If you give it a different value, an error message will appear for easy inspection.

C++ typedef declares the new type
in C++, in addition to declaring structs, shares, enumerations, and so on, you can also declare a new type name with typedef instead of an existing type such as

      typedef int INTEGER;   // specify the identifier INTEGER to represent int type       typedef float REAL;   // specifies REAL for float type

Thus, the following two lines are equivalent:

      int i,j; float a,b;       INTEGER i,j; REAL a,b;

This allows people familiar with FORTRAN to define variables in INTEGER and REAL to suit their habits.

If you are in a program where an integer variable is specifically used for counting, you can use COUNT as the integer type name:


  typedef int COUNT; // Specify the use COUNT On behalf of int type 
  COUNT i,j; // The variable i,j Is defined as COUNT type 

That is, the int type defines the variables i and j as COUNT types in the program, so that people can know more clearly that they are used for counting.

You can also declare a struct type:


typedef struct // Note that in struct The keyword was used earlier typedef , means to declare a new name 
{
  int month; int day; int year;
}DATE; // Pay attention to DATE Is the new type name, not the struct variable name 

The declared new type name DATE represents a struct type specified above. This allows you to define the variable DATE:
      DATE birthday; DATE * p;   //p is the pointer
to the data of this structure type It could go further:


  enum  Enumerated type name { Enumerate the list of constants };
0

In summary, the way to declare a new type name is:

Write the definition statement as you defined the variable (e.g. int i;) . Replace the variable name with the new type name (for example, replace i with COUNT). Add typedef in front (e.g. typedef int COUNT). You can then define the variable with the new type name.

Take the array type declared above for example:

int n[100]; Replace the variable name n with the specified type name: int NUM[100]; Add typedef in front to get typedef int NUM[100]; Used to define variables: NUM n; n is an array of 100 integer elements.

It is customary to capitalize type names declared with typedef to distinguish them from standard type identifiers provided by the system.

A few notes on typedef:

typedef can declare various type names, but cannot be used to define variables. typedef can declare array type, string type, use more convenient. Using typedef simply adds a type name to an existing type, rather than creating a new type. When using the same type of data in different source files (especially like arrays, pointer, structure, and the appropriate data types), commonly used typedef declare some data type, put them separately in a header file, and then in the need to use them file # include command is used to include them, in order to improve programming efficiency. The use of typedef is conducive to the general application and migration. Sometimes programs rely on hardware features and are portable with typedef.

Related articles: