Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the enumeration type of C++

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to define the enumerated types of C++. The content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to define enumerated types of C++. Let's take a look.

The C++ enumeration type enum represents enumerations and is usually used to define character constants of a new type, such as enum {January,February,March}. The scope of an enumerated type is generally within the entire file or class.

Define format

The definition format of enumerated types is:

Enum {}; format description: the keyword enum-- indicates that the identifier that follows is the name of an enumerated type. Enumeration constant table-consists of enumeration constants. "enumeration constant" or "enumeration member" is an integer in the form of an identifier that represents the value of an enumerated type. The enumeration constant table lists all values of enumerated types, separated by "," constants, and must be different. The value type is the same as the conditional expression. Application examples: enum color_set1 {RED, BLUE, WHITE, BLACK}; / define enumerated types color_set1enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; / / important hints for defining enumerated types week:

Enumeration constants represent the possible values of variables of this enumeration type, and the compiler assigns an integer value to each enumeration constant. By default, this integer is the sequence number of the enumerated element, starting with 0. You can specify integer values for some or all of the enumeration constants when defining the enumeration type, the enumeration constants before the specified values are still valued by default, and the enumeration constants after the specified values are valued according to the principle of adding 1 in turn. The values of each enumeration constant can be repeated. For example:

Enum fruit_set {apple, orange, banana=1,peach, grape} / / enumerate constants apple=0,orange=1, banana=1,peach=2,grape=3. The values of enum week {Sun=7, Mon=1, Tue,Wed,Thu,Fri,Sat}; / / enumeration constant Sun,Mon,Tue,Wed,Thu,Fri,Sat are 7, 1, 2, 3, 4, 5, 6, respectively.

Enumeration constants can only be expressed in the form of identifiers, not literal constants such as integers or characters. For example, the following definition is illegal:

Enum letter_set {'an enumeration constant cannot be a character constant. / / enumeration constants cannot be character constants. Enum year_set {2000, 2001, 2003, 2004, 2005}; / / enumerated constants cannot be integer constants.

It can be changed into the following form to define it legally:

Enum letter_set {a, d, F, s, T}; enum year_set {y2000, y2001, y2002, y2003, y2004, y2005}; use of enumerated variables

The main purpose of defining enumeration types is to increase the readability of the program. One of the most common and meaningful uses of enumerated types is to describe state quantities, as you will see in the input and output stream classes in Chapter 9.

Define format: after defining an enumerated type, you can define variables for that enumerated type, such as:

Color_set1 color1, color2

You can also define both the type and the variable (or even save the type name) in the following format:

Enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} weekday1, weekday2; related operations

The value of an enumerated variable can only take the value listed in the enumeration constant table, which is a subset of the integer.

Enumerated variables occupy the same amount of memory as integers.

Enumerated variables can only participate in assignment and relational operations as well as output operations with their own integer values. For example, there is a definition:

Enum color_set1 {RED, BLUE, WHITE, BLACK} color1, color2;enum color_set2 {GREEN, RED, YELLOW, WHITE} color3, color4

The allowed assignment operations are as follows:

Color3=RED; / / assigns enumeration constants to enumerated variables of the same type color4=color3; / /, the value of color4 is REDint ionomer color3; / / assigns enumerated variables to integer variables, the value of I is 1int jargon; / / assigns enumeration constants to integer variables, and the value of j is 0

Allowed relational operations are: =,!,!, etc., for example:

/ / compare whether the same type of enumeration variable color3,color4 is equal if (color3==color4) cout "equal"; / / output the comparison result of variable color3 and WHITE, and the result is 1cout

Enumerated variables can be output directly, outputting the integer value of the variable. For example:

Cout instance

There are several balls of red, yellow, blue, white and black in the pocket. Take three balls of different colors from the pocket each time, count and output all the choices.

Analysis: since the ball can only be one of the five colors, the enumeration type can be used to represent the color of the ball. Let the ball taken out be I, j, k. According to the meaning of the question, I, j, k can have five values respectively, and I ≠ j ≠ k. The exhaustive method can be used to test each possible combination one by one to find out the combination that meets the requirements and output it.

Example # include#includeusing namespace std;int main () {enum color_set {red,yellow,blue,white,black}; / / declares that the enumerated type color color_set color; int iPowerjrekcounter counter 0bookloop; / / counter is the cumulative number of combinations of different colors for (for (k=red) the first two balls differ in color. Kif (please press enter to continue) {/ / the third ball is different from the first two, meet the requirement counter++; if ((counter)% 22 balls 0) {/ / 22 lines per screen cout "Please press enter to continue"; cin.get () } coutfor (loop=1;loopcase 1: color= (color_set) I; break; / / the first is I case 2: color= (color_set) j; break; / / the second is j case 3: color= (color_set) k; break / / the third is k} switch (color) {case red: cout "red"; break; case yellow:cout "yellow"; break; case blue: cout "blue"; break Case white: cout "white"; break; case black: cout "black"; break;}} cout "all in all:"species" return 0;} important hints that enumerated variables can be output directly, but not input directly. For example: cout > > color3; / / illegal cannot directly assign constants to enumerated variables. For example: color1=1; / / illegal enumeration variables of different types cannot be assigned to each other. For example, the input and output of color1=color3; / / illegal enumerated variables generally use switch statements to convert them into characters or strings; other processing of enumerated type data often use switch statements to ensure the legitimacy and readability of the program. This is the end of the article on "how to define the enumerated types of C++". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define the enumeration types of C++". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report