[英]How can implement my custom ranged for loop?
I do really like Ranged-based-for-loop which is supported by C++11 and above. 我真的很喜欢C ++ 11和更高版本支持的Ranged-based-for-loop。 I'd like for some understanding reason to simulate it.
我希望出于某种理解的原因对其进行仿真。 Here is an example:
这是一个例子:
// 1
//#define ranged_for(X, T) \
// for (std::vector<int>::iterator beg{ T.begin() },\
// end{ T.end() }; beg != end; X = *beg, ++beg)\
// 2
//#define ranged_for(X, T) \
// for (std::vector<int>::iterator beg{ T.begin() },\
// end{ T.end() }; beg != end; ++beg, X = *beg)\
// 3
#define ranged_for(X, T) \
for (std::vector<int>::iterator beg{ T.begin() },\
end{ T.end() }; beg != end; ++beg)\
X = *beg,
int main(){
std::vector<int> data{75, 435, 6578, 92, 123};
auto i{ 0 };
ranged_for(i, data)
std::cout << i << std::endl;
std::cout << std::endl;
std::cin.get();
return 0;
}
As you can see above the first macro doesn't get the first element 75
but instead the value 0
and the last one is not there. 正如您在上面看到的,第一个宏不会获取第一个元素
75
,而是值0
和最后一个都不在那里。 That is because I guess in my main I print x
before assigning it in the post-iteration part of the loop. 那是因为我想在我的主代码中先打印
x
然后再在循环的迭代后部分分配它。
The second macro crashes the program that is because i think de-referencing the last node (sentry node). 第二个宏使程序崩溃,这是因为我认为取消引用最后一个节点(哨兵节点)。
The third works fine but as you can see after the macro expansion I'll get: 第三个工作正常,但是如您所见,在宏扩展之后,我会得到:
i = *beg, std::cout << i << std::endl;
That is because the line above is treated as a single statement. 这是因为上面的行被视为单个语句。 Is there a better way and explanation.
有没有更好的方法和解释。 Thank you all good dudes!.
谢谢大家好家伙!
Why do you oppose C++ ranged based for loops so much? 为什么您如此反对基于范围的C ++循环呢?
int i;
ranged_for(i, data)
// ...
vs. 与
for(int i : data)
You needed to predeclare i
so far. 到目前为止,您需要预先声明
i
。 You cannot use references this way! 您不能以这种方式使用引用! Now let's imagine we manage to get it cleverer:
现在让我们想象一下,我们设法使其变得更加聪明:
ranged_for(int& i, data)
vs. 与
for(int& i : data)
What did you gain? 你得到了什么? Using a comma instead of a colon???
使用逗号而不是冒号??? Honestly, that's not worth the effort.
老实说,这不值得付出努力。 A scenario more interesing to consider:
需要考虑的场景:
auto i = data.end();
for(auto j = data.begin(); j != data.end(); ++j)
{
if(someCondition)
i = j;
}
if(i != data.end())
{
// ...
}
OK, that's rather rare already. 好吧,那已经很罕见了。 In many, if not most cases you can move the outer if's body into the inner one adding a
break
instruction at the end. 在很多情况下(如果不是大多数情况下),您可以将外部if的主体移入内部,在末尾添加一个
break
指令。 And in these few cases you still cannot do so - well, then I'd live with the explicit iterator loop – it's not that heavy to write... 在这几种情况下,您仍然不能这样做-好吧, 那我就可以使用显式的迭代器循环-编写起来并不那么繁重...
This should work for both single line and multiline scopes: 这对单行和多行示波器均适用:
#define ranged_for(X, T) \
for(auto it=std::begin(T); it!=std::end(T) && (X=*it,true); ++it)
Or a version that can be used to auto deduce the type X
should be, but it requires an additional macro: 或者可以用来自动推断
X
类型的版本,但是它需要一个附加的宏:
#define ranged_for(X, T) \
{decltype(T)::value_type X; for(auto it=std::begin(T); it!=std::end(T) && (X=*it,true); ++it) {
#define range_end }}
int main(){
std::vector<int> data{75, 435, 6578, 92, 123};
ranged_for(i, data)
std::cout << i << std::endl;
range_end
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.