久久久久久久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 表達式中的變量聲明)
主站蜘蛛池模板: 91亚洲精选| 国产在线播放av | 日韩欧美一区二区三区免费观看 | 国产日韩中文字幕 | 婷婷色国产偷v国产偷v小说 | 国产精品久久久久久久久久久久冷 | av色站 | 亚洲精品视频在线 | 中文字幕一二三 | 一级毛片观看 | 国产一区二区三区四区三区四 | 亚洲乱码国产乱码精品精98午夜 | 欧美成视频 | 国产激情第一页 | 亚洲一区视频在线 | 亚洲国产精品久久久久秋霞不卡 | 国产精品久久久久无码av | 国产精品久久久久一区二区三区 | 99亚洲精品 | 最新91在线 | 欧美日韩不卡合集视频 | 国产二区三区 | 自拍偷拍小视频 | 天天影视网天天综合色在线播放 | 久久精品一区二 | 亚洲乱码国产乱码精品精的特点 | 久久专区| 国产精品久久久久9999鸭 | 午夜激情免费视频 | 久久久精品 | 日韩av啪啪网站大全免费观看 | 亚洲国产成人精品在线 | 久久99精品久久久久久国产越南 | 欧美日本一区 | 精品国产乱码久久久久久闺蜜 | 亚洲免费在线 | 国产免费一区二区三区 | 久久人人爽人人爽人人片av免费 | 日韩视频免费 | 99久久久国产精品 | 国产午夜精品一区二区三区嫩草 |