简体   繁体   English

c ++中基于范围的for循环

[英]range-based for loops in c++

It would seem that the "for each" style of syntax available in C++11 permits array iteration without knowledge of the actual size of the array (number of elements). 似乎C ++ 11中可用的“for each”语法允许在不知道数组的实际大小(元素数量)的情况下进行数组迭代。 I assume, since it is part of the new standard, that this is perfectly safe, even for C arrays. 我认为,因为它是新标准的一部分,即使对于C阵列,这也是非常安全的。 Typically, you must also separately know the size of a C array before manipulating it, but I want verification from anyone experienced with this new C++ technique that it works exactly as you'd expect: 通常,在操作C数组之前,您还必须单独知道C数组的大小,但我希望任何有这种新C ++技术经验的人都可以验证它是否与您期望的完全相同:

extern float bunch[100];

for (float &f : bunch) {
  f += someNumber;
}

Is there anything I should know about non-obvious side effects or disadvantages to this technique? 对于这种技术,我应该知道哪些非明显的副作用或缺点? It doesn't show much in code I see, probably because most of the code was written before this was in the standard. 它在我看到的代码中没有显示太多,可能是因为大多数代码都是在标准之前编写的。 Want to make sure its rare usage isn't because of some other reason not well-known. 想要确保它的罕见用法不是因为其他一些不为人知的原因。

There is nothing strange or unsafe about that usage. 这种用法没有什么奇怪或不安全的。 The array's size is known at compile time, so it can be used in the loop. 数组的大小在编译时是已知的,因此可以在循环中使用。 This is an example of a template function that allows you to find out the length of an array: 这是一个模板函数的示例,它允许您找出数组的长度:

template< class T, std::size_t N >
std::size_t length( const T (&)[N] )
{
  return N;
}

Foo f[5];
std::cout << length(f) << "\n";

This should make it clear that you cannot use this technique, or range based loops, on dynamically sized C-style arrays. 这应该清楚地表明,您不能在动态大小的C风格数组上使用此技术或基于范围的循环。

Of course, if you have range based loops then you should also have std::array (if not, you can probably get ti from std::tr1 or boost), so you can avoid the C-style array entirely: 当然,如果你有基于范围的循环,那么你也应该有std::array (如果没有,你可以从std::tr1或boost得到ti),所以你可以完全避免使用C风格的数组:

extern std::array<float, 100> bunch;

for (auto &f : bunch) {
  f += someNumber;
}

It is perfectly safe to use a range-based for-loop with arrays. 使用带阵列的基于范围的for循环是完全安全的。 I suppose you're worried that you might accidentally use it on a pointer: 我想你担心你可能会意外地在指针上使用它:

float* array = new float[100];
for (float& f : array) {
    // ...
}

Don't worry though. 不过不用担心。 The compiler will produce an error in this case. 在这种情况下,编译器将产生错误。 So in cases where it's not safe, you'll get a compilation error anyway. 因此,在不安全的情况下,无论如何都会出现编译错误。

Arrays can be passed as references and have their type and length deduced. 数组可以作为引用传递,并推导出它们的类型和长度。

#include <iostream>

template<typename T, size_t N>
void fun(T const (&arr)[N])
{
    for (std::size_t i = 0; i < N; ++i)
       std::cout << arr[i] << " ";
}

int main()
{
   int x[] = { 1, 2, 3 }; // no need to set length here
   fun(x); // 1 2 3, no need to specify type and length here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM