Is there a way to define variables of two different types in a for loop initializer?(有没有办法在 for 循环初始值设定项中定义两种不同类型的变量?)
问题描述
您可以在 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 循环初始值设定项中定义两种不同类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在 for 循环初始值设定项中定义两种不
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01