c++ enum typeIf a variable has only a few possible values,Can be defined as an enumeration type. The so-called "enumeration" refers to enumerating the values ​​of variables one by one.The value of a variable can only be within the range of values ​​listed.Declare the enumeration type with enum. E.g:

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

The above declares an enumeration type weekday. Sun, mon,…, sat, etc. in curly braces are called enumeration elements or enumeration constants.The value representing a variable of this type can only be one of the above 7 values.They are user-defined identifiers.

The general form of declaring an enumeration is:

 enum enum type name {enum constant table column};

After the enum type is declared,You can use it to define variables.Such as:

 weekday workday, week_end;

In this way, workday and week_end are defined as variables of enum type weekday.

In the C language, the enum type name includes the keyword enum. The above definition can be written as:

 enum weekday workday, week_end;

In C++, it is not allowed to write the enum, and generally does not write the enum, but the use of 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. E.g:


is correct.You can also directly define enum 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 enumeration types:

Enumeration elements are treated as constants,It is called an enumeration constant.

Enumerating elements as constants,They are valuable,C++ compilation assigns them 0, 1, 2, 3, ... in the order in which they were defined. You can also specify the value of an enumeration element when declaring an enumeration type.

Enumerated values ​​can be used for comparison.

An integer cannot be directly assigned to an enum variable.

[Example] There are several red, yellow, blue, white, and black balls in the pocket.Take 3 balls at random from your pocket each time, and ask how to get 3 different colored balls.Output of each permutation.

#include<iomanip>//The setw control character is used in the output
using namespace std;
int main ()
  enum color {red, yellow, blue, white, black};//declare enum type color
  color pri;//define variable pri of type color
  int i, j, k, n=0, loop;//n is the total 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 colors of the first two balls are different
      for (k=red;k<= black;k ++) //Only the colors of the first two balls are different,Only need to check the color of the 3rd ball
        if ((k!=i)&&(k!=j)) //The colors of the 3 balls are all different
         n=n + 1;//to increase the accumulated value n
         cout<setw (3)<n;//output the current n value, the field width is 3
         for (loop=1;loop<= 3;loop ++) //Treat 3 balls successively
           switch (loop) //The value of loop is 1, 2, 3
             case 1:pri=color (i);break;//color (i) is a cast,Make the value of pri i
             case 2:pri=color (j);break;//make the value of pri be j
             case 3:pri=color (k);break;//make the value of pri be k
      switch (pri) //judge the value of pri and 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;
  cout<"total:"<n<endl;//output the number of combinations that meet the conditions
  return 0;

The results are as follows:

1 red yellow blue 2 red yellow white 3 red yellow black
58 black white red
59 black white yellow
60 black white blue

No enumeration of constants,It is also possible to use the constant 0 for "red", 1 for "yellow" ... But obviously using enum variables is more intuitive,Because enumeration elements use "familiar" identifiers,And the value of the enumeration variable is limited to the range of several enumeration elements specified at the time of definition. If you give it another value,An error message will appear,Easy to check.

C++ typedef declare new typeIn C++, in addition to declaring structs, unions, enumerations and other types,You can also use typedef to declare a new type name instead of an existing type such as:

typedef int integer;//Specify the identifier integer for int type typedef float real;//Specify the float type as real

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 using integer and real,To adapt to their habits.

If in a program,Integer variables are specifically used for counting,You can use count as an integer type name:

 typedef int count;//Specify count for int
  count i, j;//Define variable i, j as count type

That is, the int type defines the variables i and j as count types in the program, which makes it more clear that they are used for counting.

You can also declare struct types:

typedef struct //Note that the keyword typedef is used before the struct, which means that the new name is declared
  int month;int day;int year;
} date;//Note that date is the new type name,Instead of struct variable names

The declared new type name date represents a structure type specified above.This allows you to define variables with date:

date birthday;date * p;//p is a pointer to data of this structure type

You can go further:

typedef int num [100];//Declares num as an integer array type,Contains 100 elements
num n;//Define n as an array containing 100 integer elements
typedef char * string;//Declares string as a character pointer type
string p, s [10];//p is a character pointer variable,s is an array of pointers (with 10 elements)
typedef int (* pointer) () //Declares pointer as the type of pointer to a function,Function returns an integer value
pointer p1, p2;//p1, p2 is a pointer variable of type pointer

To sum it up,The way to declare a new type name is:

First write the definition statement (such as int i;) in the same way as defining variables. Replace the variable name with a new type name (such as i with count). Add typedef (such as typedef int count) to the front. You can then use the new type name to define the variable.

Take the declaration of the above array type as an example:

First write as a defined array:int n [100]; Replace the variable name n with the type name specified by you:int num [100]; Add typedef in front and get typedef int num [100]; Used to define variables:num n;(n is an array containing 100 integer elements).

It is customary to use capital letters for type names declared with typedefs.To distinguish it from standard type identifiers provided by the system.

A few notes about typedef:

typedef can declare various type names,It cannot be used to define variables.With typedef you can declare array types, string types,Easy to use. Using typedef just adds a type name to an existing type,Without creating new types. When using the same type of data in different source files (especially type data such as arrays, pointers, structures, unions, etc.), common typedefs declare some data typesPut them separately in a header file,Then include them in the files that need them with the #include command,To improve programming efficiency. The use of typedef is conducive to the generality and portability of the program.Sometimes programs rely on hardware features,Use typedefs for easy porting.
  • Previous A guide to using reference types as function parameters in C ++ programming
  • Next Code to open sidebar menu effects based on jQuery tilt