gpt4 book ai didi

c++ - 基于范围的 for 循环等价物

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:54 26 4
gpt4 key购买 nike

所以根据n2243基于范围的 for 循环等同于:

{
auto && __range = ( expression );

for ( auto __begin = std::Range<_RangeT>::begin(__range),
__end = std::Range<_RangeT>::end(__range);
__begin != __end;
++__begin )
{
for-range-declaration = *__begin;
statement
}
}

然后它说 2 If the header <iterator_concept> is not included prior to a use of the range-based for statement, the program is ill-formed.所以我质疑这是最新的。我也很好奇什么std::Range是或者如果它纯粹是一个实现细节。我能找到的最接近的是 n3350 .

answer依赖此信息并说:

Range for is as fast as possible since it caches the end iterator[citation], uses pre-increment and only dereferences the iterator once.

so if you tend to write:

for(iterator i = cont.begin(); i != cont.end(); i++) { /**/ }

Then, yes, range-for may be slightly faster, since it's also easier to write there's no reason not to use it (when appropriate).

P.S. I said it's as fast as possible, it isn't however faster than possible. You can achieve the exact same performance if you write your manual loops carefully.

我很好奇它现在是否真的有所作为。据我所知,它只是语法糖。例如,在您可以执行 auto it = s.rbegin(); it != s.rend(); ++it 的循环中,它需要返回反向迭代器的样板代码,其中基于范围的 for 循环需要 begin。和 end .如果它所节省的只是打字,那么它还提供了哪些其他优势,因为它期望beginend ?我很好奇我上面引用的答案是否仍然有效,因为这篇论文是 2007 年的。

最佳答案

正如@DyP 所说,这部分在最终标准中发生了一些变化。 C++11 6.5.4 基于范围的for语句[stmt.ranged]:

1 For a range-based for statement of the form

  for ( for-range-declaration : expression ) statement

let range-init be equivalent to the expression surrounded by parentheses

  ( expression )

and for a range-based for statement of the form

  for ( for-range-declaration : braced-init-list ) statement

let range-init be equivalent to the braced-init-list. In each case, a range-based for statement is equivalent to

  {
auto && __range = range-init;
for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin ) {
for-range-declaration = *__begin;
statement
}
}

where __range, __begin, and __end are variables defined for exposition only, and _RangeT is the type of the expression, and begin-expr and end-expr are determined as follows:

  • if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound, respectively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of incomplete type, the program is ill-formed;

  • if _RangeT is a class type, the unqualified-ids begin and end are looked up in the scope of class _RangeT as if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration, begin-expr and end-expr are __range.begin() and __range.end(), respectively;

  • otherwise, begin-expr and end-expr are begin(__range) and end(__range), respectively, where begin and end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespace std is an associated namespace.

[ Example:

  int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;

—end example ]

2 In the decl-specifier-seq of a for-range-declaration, each decl-specifier shall be either a type-specifier or constexpr.

关于c++ - 基于范围的 for 循环等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20758064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com