列舉是一組整數常數,例如
001 | #include <iostream> |
002 | |
003 | int main() { |
004 | enum Day {Sunday, Monday, Tuesday, |
005 | Wednesday, Thursday, |
006 | Friday, Saturday}; |
007 | |
008 | std::cout << "Sunday: " |
009 | << Sunday |
010 | << std::endl; |
011 | std::cout << "Monday: " |
012 | << Monday |
013 | << std::endl; |
014 | std::cout << "Tuesday: " |
015 | << Tuesday |
016 | << std::endl; |
017 | std::cout << "Wednesday: " |
018 | << Wednesday |
019 | << std::endl; |
020 | std::cout << "Thursday: " |
021 | << Thursday |
022 | << std::endl; |
023 | std::cout << "Friday: " |
024 | << Friday |
025 | << std::endl; |
026 | std::cout << "Saturday: " |
027 | << Saturday |
028 | << std::endl; |
029 | |
030 | Day today = Wednesday; |
031 | std::cout << "Today: " |
032 | << today |
033 | << std::endl; |
034 | |
035 | return 0; |
036 | } |
037 | |
038 | /* Kaiching Chang |
039 | u0702_1.cpp |
040 | 2014-02 */ |
第 4 行,定義一個列舉型態 Day ,使用關鍵字 (keyword) enum ,後面接著型態名稱 Day ,然後大括弧中是列舉的識別字 (identifier) ,這被稱為非作用域列舉 (unscoped enumeration)
004 | enum Day {Sunday, Monday, Tuesday, |
005 | Wednesday, Thursday, |
006 | Friday, Saturday}; |
列舉常數為從 0 開始遞增的整數常數數列,因此第 30 行宣告的 today 亦為整數常數, Wednesday 是第 4 個值,所以是整數 3
030 | Day today = Wednesday; |
編譯執行,結果如下
$ g++ u0702_1.cpp |
$ ./a.out |
Sunday: 0 |
Monday: 1 |
Tuesday: 2 |
Wednesday: 3 |
Thursday: 4 |
Friday: 5 |
Saturday: 6 |
Today: 3 |
$ |
列舉也可以匿名 (anonymous) 與指定起始整數,例如
001 | #include <iostream> |
002 | |
003 | int main() { |
004 | enum {apple, banana = 11, |
005 | orange, peach = 5}; |
006 | |
007 | std::cout << "apple: " |
008 | << apple |
009 | << std::endl; |
010 | std::cout << "banana: " |
011 | << banana |
012 | << std::endl; |
013 | std::cout << "orange: " |
014 | << orange |
015 | << std::endl; |
016 | std::cout << "peach: " |
017 | << peach |
018 | << std::endl; |
019 | |
020 | return 0; |
021 | } |
022 | |
023 | /* Kaiching Chang |
024 | u0702_2.cpp |
025 | 2014-02 */ |
此例的列舉沒有識別字,另外將 banana 設定為 11 ,因此 orange 就由 11 遞增為 12 ,最後的 peach 則設定為 5
004 | enum {apple, banana = 11, |
005 | orange, peach = 5}; |
編譯後執行,結果如下
$ g++ u0702_2.cpp |
$ ./a.out |
apple: 0 |
banana: 11 |
orange: 12 |
peach: 5 |
$ |
列舉也可以跟 struct 或 class 一起宣告,形成作用域列舉 (scoped enumeration) ,例如
enum class Color {RED, GREEN, BLUE}; |
C++11 中,列舉常數可以改用其他型態,此時要在列舉識別字後面加上冒號及型態名稱,舉例如下
001 | #include <iostream> |
002 | |
003 | enum class Color: char { |
004 | RED = 'r', |
005 | GREEN = 'g', |
006 | BLUE = 'b', |
007 | }; |
008 | |
009 | int main() { |
010 | Color r; |
011 | r = Color::RED; |
012 | Color g; |
013 | g = Color::GREEN; |
014 | Color b; |
015 | b = Color::BLUE; |
016 | |
017 | std::cout << "RED: " |
018 | << static_cast<char>(r) |
019 | << std::endl; |
020 | std::cout << "GREEN: " |
021 | << static_cast<char>(g) |
022 | << std::endl; |
023 | std::cout << "BLUE: " |
024 | << static_cast<char>(b) |
025 | << std::endl; |
026 | |
027 | return 0; |
028 | } |
029 | |
030 | /* Kaiching Chang |
031 | u0702_3.cpp |
032 | 2014-02 */ |
這裡定義一個作用域列舉,並且將列舉常數的型態指定為 char
003 | enum class Color: char { |
004 | RED = 'r', |
005 | GREEN = 'g', |
006 | BLUE = 'b', |
007 | }; |
編譯後執行,結果如下
$ g++ u0702_3.cpp -std=c++0x |
$ ./a.out |
RED: r |
GREEN: g |
BLUE: b |
$ |
continue ...
沒有留言:
張貼留言