The use of enum enumeration in C ++ programming language is hard to grasp. Today, we have provided four simple code examples to help you understand the specific meaning and application methods of the C ++ enum enumeration, so as to facilitate your practical application in the future.
- How to parse the C ++ operation clipboard
- Detailed Implementation of C ++ string replacement Functions
- Introduction to multiple implementation techniques of the C ++ Singleton Mode
- What is the difference between the C ++ pointer and the reference?
- How to Implement C ++ file stream operations
C ++ enum enumeration format:
Enum <Enumeration type name >{< enumeration table >}; (see example 1)
Enum {<enumeration table >}< variable table>; (see example 2)
The first C ++ enum enumeration value corresponds to an integer. Generally, the first enumeration value corresponds to a constant value of 0, which is then low. (special usage Example 3 Example 4)
Enum bool {false, true}; bool type is C ++ pre-defined Enumeration
Example code of C ++ enum enumeration:
- # Include <iostream. h>
-
- Void main ()
- ...{
- // Example 1
- Enum day... {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; // defines the enumeration of a day
- Day d = Sun;
// D can only be equal to one of "Sun, Mon, Tue, Wed, Thu, Fri, Sat". There is no other value!
- Cout <"value is" <d <endl;
-
- // You can also define multiple variables.
- Enum day... {Sun, Mon, Tue, Wed, Thu, Fri, Sat };
- Day d1, d2, d3;
- D1 = Thu; d2 = Sat; d3 = Tue;
- Cout <"d1 & d2 & d3" <d1 <"" <d2 <"" <d3 <endl;
-
- // Example 2
- Enum... {Sun, Mon, Tue, Wed, Thu, Fri, Sat} d;
- D = Mon;
- Cout <"value is" <d <endl;
-
- // Example 3
- Enum... {Sun = 10, Mon, Tue, Wed, Thu, Fri, Sat} d1, d2, d3, d4;
- D1 = Sun; d2 = Mon; d3 = Tue; d4 = Wed;
- Cout <d1 <"" <d2 <"" <d3 <"" <d4 <endl;
// Output 10 11 12 13
-
- // Example 4
- Enum... {Sun = 10, Mon = 1, Tue, Wed, Thu, Fri, Sat} d1, d2, d3, d4;
- D1 = Sun; d2 = Mon; d3 = Tue; d4 = Wed;
- Cout <d1 <"" <d2 <"" <d3 <"" <d4 <endl;
// Output 10 1 2 3
- }
The above is an introduction to the C ++ enum enumeration.