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

將變量與多個值進行比較的最有效方法?

Most efficient way to compare a variable to multiple values?(將變量與多個值進行比較的最有效方法?)
本文介紹了將變量與多個值進行比較的最有效方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的程序中有幾次,我不得不檢查變量是否是眾多選項之一.例如

A few times in my program, I've had to check if a variable was one of many options. For example

if (num = (<1 or 2 or 3>)) { DO STUFF }

我搞砸了OR",但似乎沒有什么是正確的.我試過了

I've messed around with 'OR's, but nothing seems to be right. I've tried

if (num == (1 || 2 || 3))

但它什么都不做.

我想方便地區分幾個組.例如

I'd like to conveniently distinguish between several groups. For example

if (num = (1,2,3))

else if (num = (4,5,6))

else if (num = (7,8,9))

推薦答案

如果您要檢查的值足夠小,您可以創建您要查找的值的位掩碼,然后檢查要設置的位.

If the values you want to check are sufficiently small, you could create a bit mask of the values that you seek and then check for that bit to be set.

假設您關心幾個組.

static const unsigned values_group_1 = (1 << 1) | (1 << 2) | (1 << 3);
static const unsigned values_group_2 = (1 << 4) | (1 << 5) | (1 << 6);
static const unsigned values_group_3 = (1 << 7) | (1 << 8) | (1 << 9);    
if ((1 << value_to_check) & values_group_1) {
  // You found a match for group 1
}
if ((1 << value_to_check) & values_group_2) {
  // You found a match for group 2
}
if ((1 << value_to_check) & values_group_3) {
  // You found a match for group 3
}

這種方法最適合不超過您的 CPU 喜歡使用的自然大小的值.在現代,這通常是 64,但可能會因環境的具體情況而異.

This approach works best for values that don't exceed the natural size your CPU likes to work with. This would typically be 64 in modern times, but may vary depending upon the specifics of your environment.

這篇關于將變量與多個值進行比較的最有效方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 表達式中的變量聲明)
主站蜘蛛池模板: 国产一区精品在线 | 美国一级大黄一片免费中文 | 国产免费成人 | 日本香蕉视频 | 91蜜桃婷婷狠狠久久综合9色 | 婷婷狠狠 | 久久久精品在线观看 | 97青青草| 四虎影院www | 国产伦精品一区二区免费 | 日韩在线观看中文字幕 | 婷婷综合五月天 | 久久一二三区 | 亚欧洲精品在线视频免费观看 | 欧美日韩中文字幕在线观看 | 福利一区福利二区 | 国产一区二区网站 | 国内自拍xxxx18| 亚洲视频网 | 国产亚洲久一区二区 | 国产精品久久久久久久久久久久午夜片 | 成年人免费在线视频 | 亚洲欧美一区二区三区在线 | 亚洲精品一区二三区 | 亚洲精品在线视频观看 | 婷婷俺也去| 日韩亚洲欧美在线 | 久久久久一| 欧美午夜精品一区二区三区 | 高潮一区二区三区乱码 | 天天摸夜夜操 | 国产精品一区久久 | 日韩欧美在线一区 | 伊人国产女| 黄网站免费大全入口 | 在线观看视频国产 | 国产精品免费一区二区三区 | 天堂va蜜桃一区二区三区 | 日韩午夜片 | 欧美福利一区 | 小sao货撅起屁股扒开c微博 |