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

如何在 bool 值上編寫 `for` 循環(false 和 true)

How to write a `for` loop over bool values (false and true)(如何在 bool 值上編寫 `for` 循環(false 和 true))
本文介紹了如何在 bool 值上編寫 `for` 循環(false 和 true)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

一個主要是為了好玩/好奇的問題:如何在 C++ 中編寫一個 for 循環來迭代 bool 的兩個值(即 truefalse),僅使用 bool 的操作(即不轉換為其他類型)?

A question mostly for fun/curiosity: how to write a for loop in C++ that would iterate over two values of a bool (i.e. true and false), using only operations with bool (i.e. without conversions to other types)?

背景是我想檢查像 (A && B) || 這樣的方程有多少解(!B && !C && !D) == true,然后開始寫類似 for (bool A=false; ??? ; ++A) for (bool B=false; ...) 等,但立即被 ??? 卡住 - 即繼續循環的條件是什么?當然,我將它改寫為使用 int,而且我也知道 do ... while 循環會起作用,但是我很好奇是否有可能編寫這樣的 for 循環?由于 SO 似乎沒有答案,我決定問:)

The background is that I wanted to check how many solutions exists for an equation like (A && B) || (!B && !C && !D) == true, and started to write something like for (bool A=false; ??? ; ++A) for (bool B=false; ...) etc but immediately got stuck by ??? - i.e. what would be the condition to continue the loop? Of course I rewrote it to use int, and I also know that a do ... while loop will work, but I got curious if it's ever possible to write such a for loop? And since SO does not seem to have an answer, I decided to ask :)

更新:請注意,在至少兩個現已刪除的答案中建議的明顯"變體 for(bool A=false; !A; A=true) 只會運行一次迭代,因為對于第二個條件 !A 變為 false 并且循環結束.

Update: note that an "obvious" variant for(bool A=false; !A; A=true) suggested in at least two now-removed answers will only run one iteration, because for the second one the condition !A becomes false and the loop ends.

經過深思熟慮后,我相信在沒有第二個變量或像 Dietmar Kühl 建議的基于指針的構造的情況下,在 C++03 中是不可能做到的.條件應在所需的執行中測試 3 次,因此布爾值的兩個值根本不夠.do-while 循環之所以有效,是因為第一次迭代是無條件執行的,條件只檢查兩次,因此可以使用 bool 值在繼續和退出之間進行選擇.

After some pondering, I believe it's impossible to do it in C++03 without a second variable or a pointer based construct like suggested by Dietmar Kühl. The condition should be tested three times in a desired execution, so two values of a bool are simply not enough. And the do-while loop works because the first iteration is executed unconditionally, the condition is only checked twice and so a bool value can be used to select between continuing and exiting.

推薦答案

C++11: for (bool b : { false, true }) {/* ... *

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

相關文檔推薦

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 表達式中的變量聲明)
主站蜘蛛池模板: 中文字幕在线观看一区二区三区 | 中文字幕丰满人伦在线 | 22精品一区二区三区 | 国产免费黄色片 | 天天操夜夜摸 | 伊人影院久久 | 久久久网| 国产精自产拍久久久久久蜜 | 四虎看片 | 日韩国产一区二区 | 国产在线第一页 | 欧美日在线 | 国产福利视频在线 | 国产精品久久久久久久久久久久午夜片 | 亚洲在线视频观看 | 欧美久久久久久久久 | 亚洲一区二区在线视频 | 黄色三级免费 | 一区二区三区国产精品 | 欧美成人猛片aaaaaaa | 欧美日韩小视频 | 激情高潮到大叫狂喷水 | 在线黄色网| 亚洲视频在线视频 | 久久人人爽 | 黄色一级视频 | 日韩av在线免费看 | 天天搞天天干 | 色综合久久天天综合网 | 天堂中文av | 黄色一级小说 | 日本一本视频 | 在线观看h片 | av黄色在线| 在线看片a | 精品国产欧美 | 日日日操操操 | 日韩欧美二区 | 亚洲福利视频一区 | 国产精品999| 亚洲国产第一页 |