問題描述
您可以在 for 循環中定義 2 個相同類型的變量:
You can define 2 variables of the same type in a for loop:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
但是定義不同類型的變量是非法的:
But it is illegal to define variables of different types:
int main() {
for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
有沒有辦法做到這一點?(我不需要在循環內使用 i
,只需使用 j
.)
Is there a way to do this? (I don't need to use i
inside the loop, just j
.)
如果您有完全被黑和晦澀的解決方案,那對我來說沒問題.
If you have totally hacked and obscure solution, It's OK for me.
在這個人為的例子中,我知道你可以只對兩個變量使用 double
.我正在尋找一個通用的答案.
In this contrived example I know you could just use double
for both variables. I'm looking for a general answer.
請不要建議將任何變量移到 for 主體之外,可能對我來說不可用,因為它是一個迭代器,它必須在循環后立即消失,而 for 語句將包含在我的 foreach 中
宏:
Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach
macro:
#define foreach(var, iter, instr) {
typeof(iter) var##IT = iter;
typeof(iter)::Element var = *var##IT;
for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) {
instr;
}
}
可以這樣使用:
foreach(ii, collection, {
cout << ii;
}).
但我需要一些像這樣使用的東西:
But I need something that will be used like that:
foreach(ii, collection)
cout << ii;
請不要引入任何運行時開銷(但編譯可能會很慢).
Please do not introduce any runtime overhead (but it might be slow to compile).
推薦答案
這是一個使用 boost 預處理器的版本(這只是為了好玩.有關真實世界的答案,請參閱上面@kitchen 的答案):
Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see @kitchen's one above):
FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) {
}
第一部分指定了一系列聲明:(a)(b)...
.后面聲明的變量可以引用之前聲明的變量.第二和第三部分和往常一樣.在第二部分和第三部分出現逗號的地方,可以使用括號來防止它們分隔宏參數.
The first part specifies a sequence of declarations: (a)(b)...
. The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.
我知道有兩個技巧用于聲明變量,這些變量后來在添加到宏之外的復合語句中可見.第一個使用條件,如 if:
There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:
if(int k = 0) ; else COMPOUND_STATEMENT
然后 k
是可見的.自然地,它總是必須評估為 false
.所以它不能被我們使用.另一個上下文是這樣的:
Then k
is visible. Naturally, it always have to evaluate to false
. So it can't be used by us. The other context is this one:
for(int k = 0; ...; ...) COMPOUND_STATEMENT
這就是我要在這里使用的.我們必須注意只對 COMPOUND_STATEMENT
進行一次迭代.執行增量和條件檢查的實際 for
循環必須放在最后,因此附加的復合語句屬于它.
That's what i'm going to use here. We'll have to watch to only make one iteration of COMPOUND_STATEMENT
. The actual for
loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.
#include <boost/preprocessor.hpp>
#include <iostream>
#define EMIT_DEC_(R,D,DEC)
for(DEC; !_k; )
#define FOR(DECS, COND, INC)
if(bool _k = false) ; else
BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS)
for(_k = true; COND; INC)
int main() {
FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
std::cout << j << std::endl;
}
}
它創建了一堆 for
語句,每個語句嵌套在另一個語句中.它擴展為:
It's creating a bunch of for
statements, each nested into another one. It expands into:
if(bool _k = false) ; else
for(int i = 0; !_k; )
for(float j = 0.0f; !_k; )
for(_k = true; i < 10; (i += 1, j = 2 * i)) {
std::cout << j << std::endl;
}
這篇關于有沒有辦法在 for 循環初始值設定項中定義兩種不同類型的變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!