久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

允許基于范圍的 For 與枚舉類?

Allow for Range-Based For with enum classes?(允許基于范圍的 For 與枚舉類?)
本文介紹了允許基于范圍的 For 與枚舉類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個循環代碼塊,其中我循環了 enum 類 的所有成員.

I have a recurrent chunk of code where I loop over all the members of an enum class.

與新的 基于范圍的 for 相比,我目前使用的 for 循環看起來非常笨拙.

The for loop that I currently use looks very unwieldly compared to the new range-based for.

有什么方法可以利用 C++11 的新特性來減少當前 for 循環的冗長?

Is there any way to take advantage of new C++11 features to cut down on the verbosity for my current for loop?

我想改進的當前代碼:

enum class COLOR
{
    Blue,
    Red,
    Green,
    Purple,
    First=Blue,
    Last=Purple
};

inline COLOR operator++( COLOR& x ) { return x = (COLOR)(((int)(x) + 1)); }

int main(int argc, char** argv)
{
  // any way to improve the next line with range-based for?
  for( COLOR c=COLOR::First; c!=COLOR::Last; ++c )
  {
    // do work
  }
  return 0;
}

換句話說,如果我能做這樣的事情就好了:

In other words, it would be nice if I could do something like:

for( const auto& c : COLOR )
{
  // do work
}

推薦答案

用枚舉本身作為迭代器來迭代枚舉是一個糟糕的主意,我建議使用實際的迭代器,如 deft_code 的答案.但如果這真的是你想要的:

Iterating enumerations with the enumeration itself as an iterator is a poor idea, and I recommend using an actual iterator as in deft_code's answer. But if this is really what you want:

COLOR operator++(COLOR& x) {
    return x = (COLOR)(std::underlying_type<COLOR>::type(x) + 1); 
}

COLOR operator*(COLOR c) {
    return c;
}

COLOR begin(COLOR r) {
    return COLOR::First;
}

COLOR end(COLOR r) {
    COLOR l=COLOR::Last;
    return ++l;
}

int main() { 
    //note the parenthesis after COLOR to make an instance
    for(const auto& c : COLOR()) {
        //do work
    }
    return 0;
}

在這里工作:http://ideone.com/cyTGD8

const COLOR COLORS[] = {COLOR::Blue, COLOR::Red, COLOR::Green, COLOR::Purple};
const COLOR (&COLORREF)[(int)COLOR::Last+1] = COLORS;

int main() { 
    for(const auto& c : COLORS) {
        //do work
    }
    return 0;
}

如下所示:http://coliru.stacked-crooked.com/a/5d356cc91556d6ef

(如果顏色數量與數組中的元素數量不匹配,單獨的定義和數組的引用會導致編譯器錯誤.非常容易安全檢查.)

(The separate defintinion and the reference of the array makes it a compiler error if the number of colors doesn't match the number of elements in the array. Excellent easy safety check.)

這篇關于允許基于范圍的 For 與枚舉類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 天天综合天天 | 国产伦精品一区二区三区精品视频 | 精品国产伦一区二区三区观看体验 | 日美女逼逼 | 337p日本欧洲亚洲大胆精蜜臀 | 国产精品高清在线 | 成人在线一区二区三区 | 97超碰站 | 亚洲视频免费在线观看 | 91.色| 一区二区三区中文字幕 | 久久国产区| 亚洲精品乱码久久久久久蜜桃91 | 亚洲国产一区二区三区 | a亚洲精品 | 一区二区三| 国产精品亚洲欧美日韩一区在线 | 99免费精品视频 | 国产欧美日韩一区二区三区在线观看 | 中文字幕亚洲欧美日韩在线不卡 | 日本一区二区三区四区 | 精品自拍视频在线观看 | 久久毛片网站 | 日本久久一区 | 欧美激情视频一区二区三区在线播放 | 91中文在线观看 | 精品视频一区二区 | 91精品久久久久久久久中文字幕 | 国产成人久久久 | 国产区在线视频 | 日本三级电影在线看 | 91精品久久久久久久久中文字幕 | 日韩精品免费 | 日韩美女在线看免费观看 | 国产在线一区二区 | 中文字幕精品一区二区三区精品 | 一级片在线视频 | 成人影 | 国产精品久久久久久久久污网站 | 99精品亚洲国产精品久久不卡 | 欧美精品一区二区三区在线播放 |