問題描述
我希望 for 循環中的迭代器變量作為 unsigned int
反向迭代到 0,我想不出與 i > 類似的比較.-1
,就像如果它是 signed int
一樣.
I want the iterator variable in a for loop to reverse iterate to 0 as an unsigned int
, and I cannot think of a similar comparison to i > -1
, as you would do if it was a signed int
.
for (unsigned int i = 10; i <= 10; --i) { ... }
但這似乎很不清楚,因為它依賴于無符號整數的數值溢出超過 10.
But this seems very unclear, as it is relying on the numerical overflow of the unsigned integer to be above 10.
也許我只是沒有清醒的頭腦,但有什么更好的方法可以做到這一點...
Maybe I just don't have a clear head, but whats a better way to do this...
免責聲明:這只是一個簡單的用例,10 的上限是微不足道的,它可以是任何東西,并且 i
必須是一個 unsigned int代碼>.
Disclaimer: this is just a simple use case, the upper limit of 10 is trivial, it could be anything, and i
must be an unsigned int
.
推薦答案
可以使用
for( unsigned int j = n; j-- > 0; ) { /*...*/ }
它從 n-1
向下迭代到 0
.
It iterates from n-1
down to 0
.
這篇關于帶 for 循環的無符號 int 反向迭代的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!