简体   繁体   English

C ++中基于范围的for循环的替代方法

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

Is there any alternative to a range-based for loop when it comes to vector arrays? 在向量数组方面,基于范围的for循环还有其他选择吗? I've noticed that c++98 won't allow range-based for loops. 我注意到c ++ 98不允许基于范围的for循环。 Here is some sample code looping through a vector array using a range based for loop: 这是一些使用基于范围的for循环遍历向量数组的示例代码:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vecArray1 (3,20);
  for (int v: vecArray1) {
    cout << "ArrayValue:" << v << endl;
  }
  return 0;
}

Now here is an alternative I've tried that didn't work: 现在这是我尝试过的一种替代方法,它无效:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vecArray1 (3,20);
  for (int i=0; i<sizeof(vecArray1); ++i) {
    cout << "ArrayValue:" << vecArray1[i] << endl;
  }
  return 0;
}

It outputs 10 elements instead of 3 defined by the vector array above. 它输出10个元素,而不是上面的向量数组定义的3个元素。 Are there any alternatives to range-based for loops that work with c++98? 在c ++ 98下,基于范围的for循环是否有其他选择?

sizeof(vecArray1) doesn't do what you think it does. sizeof(vecArray1)并没有您认为的那样。

Either use: 可以使用:

for (int i=0; i<vecArray1.size(); ++i) {
    cout << "ArrayValue:" << vecArray1[i] << endl;
}

or: 要么:

for (std::vector<int>::iterator it = vecArray1.begin(); it != vecArray1.end(); it++) {
    cout << "ArrayValue: " << *it << endl;
}

The second one is more verbose, but works for other types of collections as well (like std::list ) and is more similar to what the range-based for loop does. 第二个更冗长,但也适用于其他类型的集合(如std::list ),并且与基于范围的for循环更相似。

C++98 does not allow for range-based for-loops. C ++ 98不允许基于范围的for循环。 In C++98 you would need to do the following: 在C ++ 98中,您需要执行以下操作:

for(unsigned int i=0;i<vecArray.size();++i)
    std::cout << "ArrayValue: " << vecArray[i] << std::endl;

or 要么

for(std::vector<int>::iterator it=vecArray.begin();it!=vecArray.end();++it)
    std::cout << "ArrayValue: " << *it << std::endl;

The operator 运营商

sizeof

does NOT give you the length of an array. 不能给你数组的长度。 Instead, it returns an unsigned integer representing the number of bytes of the type you give as the argument. 而是返回一个无符号整数,该整数表示您作为参数给出的类型的字节数。 For example, 例如,

std::cout << sizeof(unsigned long long) << std::endl;

prints 版画

8

on my machine, because the type unsigned long long consista of 64-bits, or 64/8 = 8 bytes. 在我的机器上,因为类型unsigned long long由64位或64/8 = 8个字节组成。

In C++11 we now have range-based for-loops: 在C ++ 11中,我们现在有基于范围的for循环:

Examples: 例子:

for(int i : vecArray)
    std::cout << "i = " << i << std::endl;

for(const int& i : vecArray)
    std::cout << "i = " << i << std::endl;

In the first example the values are copied from vecArray into the variable i. 在第一个示例中,将值从vecArray复制到变量i中。 In the second example you are instead working with const references to the values in vecArray (which could be useful for objects which are expensive to copy). 在第二个示例中,您将使用const引用vecArray中的值(这对于复制成本很高的对象可能很有用)。

In fact, the range-based for loops in C++11 are available for all types on which you can call begin() and end() (ie those which you can iterate through). 实际上,C ++ 11中基于范围的for循环适用于可以调用begin()和end()的所有类型(即可以迭代的类型)。

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

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