c++中增强的FOR循环

Enhanced FOR loops in C++

本文关键字:循环 FOR 增强的 c++      更新时间:2023-10-16

我正在从Java切换到c++,我想知道c++是否包含我在Java中使用的增强的for循环,例如:

int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
  System.out.println("Count is: " + item);
}

这个相同的"快捷方式"在c++中可能吗?

c++ 11可以。它们被称为基于范围的fors。请记住,应该将类型限定为引用或对const的引用。

c++ 03的解决方案是BOOST_FOR_EACH或boost::bind与std::for_each结合使用。使用Boost.Lambda可以实现更奇特的功能。如果你想让你自己或你的同事失望,我推荐不推荐的文件夹std::bind1ststd::bind2nd

下面是一些示例代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <boost/lambda/lambda.hpp>
#include <functional>    
int main()
{
  int i = 0;
  std::vector<int> v;
  std::generate_n(std::back_inserter(v), 10, [&]() {return i++;});
  // range-based for
  // keep it simple
  for(auto a : v)
    std::cout << a << " ";
  std::cout << std::endl;
  // lambda
  // i don't like loops
  std::for_each(v.begin(), v.end(), [](int x) { 
      std::cout << x << " ";
    });
  std::cout << std::endl;
  // hardcore
  // i know my lib
  std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  // boost lambda
  // this is what google came up with
  // using for the placeholder, otherwise this looks weird
  using namespace boost::lambda;
  std::for_each(v.begin(), v.end(), std::cout << _1 << " ");
  std::cout << std::endl;
  // fold
  // i want to be a haskell programmer
  std::accumulate(v.begin(), v.end(), std::ref(std::cout), 
                  [](std::ostream& o, int i) -> std::ostream& { return o << i << " "; });
  return 0;
}

在c++ 11中,如果你的编译器支持它,它是。它被称为基于范围的for

std::vector<int> v;
// fill vector
for (const int& i : v) { std::cout << i << "n"; }

它适用于C风格的数组和任何类型的函数begin()end()返回迭代器。例子:

class test {
    int* array;
    size_t size;
public:
    test(size_t n) : array(new int[n]), size(n)
    {
        for (int i = 0; i < n; i++) { array[i] = i; }
    }
    ~test() { delete [] array; }
    int* begin() { return array; }
    int* end() { return array + size; }
};
int main()
{
    test T(10);
    for (auto& i : T) {
        std::cout << i;   // prints 0123456789
    }
}

在c++ 03中没有这种可能性。然而新的标准(c++ 11)却有这个功能。参见示例(取自维基百科):

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

也可以考虑使用std::vector<int>而不是普通数组。这是对C数据类型的c++类比,这使生活更容易。

是和不是。

1。本地数组:没有,但是你可以很容易地找到

如果你有一个本地数组(int numbers[4] = {1, 2, 3, 4];),那么你可以做size = sizeof(numbers) / sizeof(int)

2。指向数组的指针:一点也不,你必须单独传递大小

如果你有一个指向数组(int* numbers = new int[4];)的指针,那么你无法计算出它的大小,除非你自己跟踪它。(或者如果在c字符串的情况下它是null终止的,但随后你必须迭代它,这是线性运行时间…)

注意,我不认为指向数组的指针是正确的术语,实际上你只是有一个指向数组第一个元素的指针,但已经为多个值分配了空间。不知道这叫什么。也许只是一个指针?

3。STL容器:是的,您可以使用迭代器执行一些for循环魔术,或者通过获取size

来使用索引。

如果你有一个向量(std::vector<int> v(3, 0);),那么你可以通过以下方式迭代它:

c++ 11:

auto it = v.begin();
for (auto it = v.begin(); it != v.end(); it++)
{
    UseElement(*it);
}

或者显然地(也是c++ 11,谢谢jrok):

for (const int& i : v) { UseElement(i); }
c++ (pre-11):

std::vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
{
    UseElement(*it);
}

或者使用索引:

for (int i = 0; i < v.size(); i++)
{
    UseElement(v[i]);
}

此外,您可以使用std算法的for_each (#include <algorithm>)对STL容器使用函数指针或函子,如下所示:

void foo(int i)
{
    std::cout << i;
}
{
    std::for_each(myvector.begin(), myvector.end(), foo);
}

其他人已经提到这种循环样式是在c++ 11中添加的。然而c++ 11甚至更好:

for (auto const& item: numbers)
{
  std::cout << "Count is: " << item << 'n';
}

这样,如果稍后将numbers的元素类型从int更改为long,甚至更改为您自己编写的某些bigint类,则根本不需要更改for循环

在旧标准c++ 03(从2003年开始)中,该语言没有内置对这种for循环的支持。你可以在Boost中使用一些技巧,但在我看来,不值得为这个小小的便利特性包含一个全新的库。

在新标准c++ 11(去年夏天刚刚发布)中,这是可能的;语法如下所示:

MyType array[] = { ... }
for (MyType& x : array) {
    ...
}

请注意,我使用MyType& x,而不是MyType x。在Java中,一切都是引用。在c++中,引用必须是显式的,并且使用&声明它们。如果不使用引用,for循环将把数组的每个元素复制到x中(这样做可能代价很高)。

然而,大多数编译器还没有完全支持c++ 11。我认为微软的Visual c++支持这个特性,但我不确定。

我发现这个简单的宏非常有用。我的绝大多数for循环都涉及到在STL容器上迭代:

#define For(it, container) for( typeof((container).begin()) it = (container).begin(); it != (container).end(); ++it)

一个例子:

vector<int> vector_of_ints;
... // initialize it somehow
For(integer, vector_of_ints) {
    cout << *integer << endl;
}
对于

,有两点需要注意:首先,它是一个迭代器,因此必须对其解引用。其次,For的第二个参数将被多次求值。我也尝试过其他的方法,但我还是坚持使用这种简单的方法。