久久久久久久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 表達式中的變量聲明)
主站蜘蛛池模板: 欧美午夜精品一区二区 | 国产一区在线视频 | 成人玩具h视频 | 一级看片免费视频 | 亚洲高清视频在线 | 色啪视频| 午夜精品免费 | 爱爱免费网站 | 久久精品一区二区三区四区 | 精品一区视频 | 午夜快播 | h视频免费在线观看 | 亚洲欧美成人 | 久久天天躁狠狠躁夜夜躁2014 | 欧美久久一区二区 | 日韩欧美网站 | 国产黄色录像 | 一本久久道 | 高清免费av | 色婷婷18| 午夜精品久久久久久久久久久久 | 蜜臀久久99精品久久久久宅男 | 91亚色视频 | 手机在线免费av | 精品第一页 | 欧美在线小视频 | 欧美 日韩 国产 成人 在线 | 国产激情综合五月久久 | 成人毛片在线 | 国产一级黄色大片 | 午夜在线小视频 | 欧美mv日韩mv国产网站 | 一区二区视频在线播放 | 丁香激情五月 | 亚洲经典一区二区三区 | 午夜精品久久 | 国产精品一区二区三 | 久久精选视频 | 欧美视频精品 | 日韩精品在线一区 | 天天爽天天操 |