[英]C++ iterate an array of integers whose size is unknown?
I have the following array: 我有以下数组:
int* myArray = new int[45];
If I wanted to iterate each element without knowing the actual size of the array, I would need to use a for_each
? 如果我想在不知道数组实际大小的情况下迭代每个元素,则需要使用for_each
?
If so, then how would you write the for_each
? 如果是这样,那么您将如何编写for_each
? I was looking over the following site and reading up on for_each but can't figure out how to put this together. 我一直在浏览以下站点并阅读for_each,但不知道如何将它们组合在一起。
http://www.cplusplus.com/reference/algorithm/for_each/ http://www.cplusplus.com/reference/algorithm/for_each/
Update: A for_each
is not a good choice in this case, due to the fact that the size of the array has to be known. 更新:由于必须知道数组的大小,因此在这种情况下, for_each
不是一个好的选择。 vectors are the proper way to accomplish such task. 向量是完成此类任务的正确方法。 My reason for using arrays, in this case, was for learning purposes. 在这种情况下,使用数组的原因是出于学习目的。 if this was a serious project I would move to something such as Lists/Vectors. 如果这是一个认真的项目,我将使用诸如列表/向量之类的东西。
Note when the question was first posted, the array in question was declared as 请注意 ,问题首次发布时,所讨论的数组被声明为
int myArray[45];
This answer deals with that particular case. 该答案处理该特定情况。
If you have C++11 support, you can use a range based loop: 如果您具有C ++ 11支持,则可以使用基于范围的循环:
for (int& i : myArray) {
std::cout << i << "\n";
}
C++11 also provides std::begin and std::end , which you can use with a fixed size array to obtain iterators: C ++ 11还提供了std :: begin和std :: end ,您可以将其与固定大小的数组一起使用以获得迭代器:
std::for_each(std::begin(myArray), std::end(myArray), <func>);
Another option, which works for C++03 and you are dealing with fixed size arrays, is to define a function template: 另一个选项适用于C ++ 03,并且您正在处理固定大小的数组,它是定义一个函数模板:
// taken a fixed size array by reference and loop over it
template <typename T, unsigned int N>
void array_for_each( T (&a)[N]) {
for (unsigned int i = 0; i < N; ++i) {
// do something with array elements
std::cout << a[i] << " ";
}
}
int main() {
int a[5];
array_for_each(a);
}
If you use MSVC, you can use "for each." 如果使用MSVC,则可以使用“ for each”。
for each(int i in arr) {
cout << i << ' ';
}
NOTE: This only works in the block of code the array is declared in. 注意:这仅在声明数组的代码块中有效。
If not, you can also use the new range-based for loop in the C++11 standard. 如果没有,您还可以在C ++ 11标准中使用新的基于范围的for循环。
for(int i : arr) {
cout << i << ' ';
}
If you're intent upon the std::for_each: 如果您打算使用std :: for_each:
for_each(arr,arr + 10,[] (int i) {
cout << i << ' ';
});
NOTE: This requires knowledge of the size of the array (in this example, 10). 注意:这需要了解阵列的大小(在本示例中为10)。
You have described an array of int, not a class that implements a InputIterator
, which is what the for_each is designed for, even though you can use it to iterate an array, but you need to know the size of the array to iterate it. 您已经描述了一个int数组,而不是一个实现InputIterator
的类,这是for_each设计的目的,尽管您可以使用它来迭代数组,但是您需要知道数组的大小才能对其进行迭代。
If you want to use for_each you need to use a vector
, list
, or implement a class that keeps track of the number of elements it contains. 如果要使用for_each,则需要使用vector
, list
或实现一个跟踪其包含的元素数量的类。 IMO it is much easier to just use a vector
IMO只需使用vector
就容易得多
If you want to just iterate your current array, assuming it is 0 terminated: 如果只想迭代当前数组,并假设其终止为0:
for(int *value = myArray; *value != 0; ++value)
printf("%d\n", *value);
Or, you can use indexes: 或者,您可以使用索引:
for(int index = 0; myArray[index] != 0; ++index)
printf("%d\n", myArray[index]);
IMO the pointer method is cleaner. IMO指针方法更干净。
This code is still dangerous though, you should either keep track of the number of records in a seperate variable, or use a vector. 但是,此代码仍然很危险,您应该跟踪单独变量中的记录数,或者使用向量。
You could use a for_each
. 您可以使用for_each
。 In this case, you have allocated space for 45 elements in your array, but since it is NULL, you'd probably get a segfault if you tried to do anything. 在这种情况下,您已经为数组中的45个元素分配了空间,但是由于它为NULL,因此如果您尝试执行任何操作,都可能会遇到段错误。 You either need to hold a value of the array, or use something like sizeof(myArray)/sizeof(myArray[0])
(which has its own problems). 您要么需要保留数组的值,要么使用诸如sizeof(myArray)/sizeof(myArray[0])
(这有其自身的问题)。
Anyway, for a for_each
here, if we actually had 45 elements: 无论如何,对于这里的for_each
,如果我们实际上有45个元素:
std::for_each(myArray, myArray + 45, <func>);
Anyway, this is part of the reason to use vectors: .begin()
and .end()
reduces errors with using incorrect indexing. 无论如何,这是使用向量的部分原因: .begin()
和.end()
通过使用不正确的索引来减少错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.