循环遍历 C++ 中的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16660237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loop through an array in c++
提问by user12074577
I want to loop through an array that I have that has a max value of 1000. I am filling the array with values from a text file. I am trying to loop through that array but in my for loop, I do not know the length of the array, so I do not know what to put in the second part of the for loop statement. For example: I have an array called: int scores[1000];and I am trying to iterate through this array and putting scores in a grade category. So A = 90-100, B = 80-89, C = 70-79, D = 60-69, F = 0-59.
我想遍历一个数组,该数组的最大值为 1000。我正在用文本文件中的值填充该数组。我试图遍历该数组,但在我的 for 循环中,我不知道数组的长度,所以我不知道在 for 循环语句的第二部分中放什么。例如:我有一个名为: 的数组int scores[1000];,我试图遍历这个数组并将分数放在一个成绩类别中。所以 A = 90-100,B = 80-89,C = 70-79,D = 60-69,F = 0-59。
So I dont know what my for loop would look like:
所以我不知道我的 for 循环会是什么样子:
for(int i = 0; i < ...; i++){
if(scores[i] > = 90 || scores[i] <= 100){
//Do stuff...
}
I guess I am also confused as to how to get the total counts of each category at the end too. But for the most part its how to iterate through this array. I know sizeof(scores[]) wont work because that will give me the int size and not the length of the array itself. Thanks though in advance!
我想我也对如何在最后获得每个类别的总数感到困惑。但在大多数情况下,它是如何迭代这个数组的。我知道 sizeof(scores[]) 不起作用,因为这会给我 int 大小而不是数组本身的长度。虽然提前谢谢!
采纳答案by Victor Sand
If you use an std::vector(link) instead, you can add elements and have the vector dynamically change size. That size can be queried easily using the size()method. If you use arrays like this, you have to keep track of the number of elements in it yourself.
如果您改用std::vector( link),则可以添加元素并使向量动态更改大小。可以使用该size()方法轻松查询该大小。如果您使用这样的数组,您必须自己跟踪其中的元素数量。
If you have a vector filles with elements your loop could look like this:
如果您有一个用元素填充的向量,您的循环可能如下所示:
std::vector<int> scores;
// fill vector
for (unsigned int i=0; i<scores.size(); i++) {
// use value
}
If you have to use arrays and actually have a scoreCountvariable with the number of real values put in there, simply use that in your loop:
如果您必须使用数组并且实际上有一个scoreCount包含实际值数量的变量,只需在循环中使用它:
for (int i=0; i<scoreCount; i++) {
// use value
}
A third option, as I mentioned in the comments, would be initializing the whole array with a value that you're never using (typically -1) and then use that as a marker for filled vs empty array positions like so:
正如我在评论中提到的,第三个选项是使用您从未使用过的值(通常为 -1)初始化整个数组,然后将其用作填充与空数组位置的标记,如下所示:
for (int i=0; i<1000; i++) {
scores[i] = -1;
}
// add real values to scores
int i=0;
while (scores[i] != -1 && i < 1000) {
// use value
i++;
}
回答by shengy
Actually the sizeof()should be done like this:
实际上sizeof()应该这样做:
sizeof(scores) / sizeof(scores[0])
sizeof(scores) / sizeof(scores[0])
And this will give you the total element numbers of the array.
这将为您提供数组的总元素数。
回答by Florian Wolters
If you really want to use a container with a fixed size, use std::arrayfor modern C++ instead of a C-array:
如果您真的想使用固定大小的容器,请使用std::array现代 C++ 而不是 C 数组:
#include <array>
std::array<std::int32_t, 1000> scores;
for (std::size_t i{0}; i < scores.size(); ++i) {
// Do stuff...
}
Otherwise use a std::vector:
否则使用std::vector:
#include <vector>
std::vector<std::int32_t> scores;
for (std::size_t i{0}; i < scores.size(); ++i) {
// Do stuff...
}
If you are able to use C++11 I also recommend to use the fixed width integer types.
如果您能够使用 C++11,我还建议您使用固定宽度的整数类型。
回答by paddy
When you populate the scoresarray, you need to actually count how many items you put in it. Then you remember that number and use it for iteration later. For example, you may have read your scores like this:
填充scores数组时,您需要实际计算放入其中的项目数。然后你记住这个数字并在以后的迭代中使用它。例如,您可能已经阅读了这样的分数:
// Read some scores: Stop when -1 is entered, an error occurs, or 1000 values are read.
int num_scores = 0;
for( ; num_scores < 1000; num_scores++ )
{
if( !(cin >> scores[num_scores]) || scores[num_scores] == -1 ) break;
}
// Do stuff with scores...
for(int i = 0; i < num_scores; i++) {
...
}
There are some other options to consider:
还有一些其他的选择需要考虑:
- use a sentinel value to represent the end of data, such as a score of -1.
- use a
std::vectorinstead.
- 使用哨兵值来表示数据的结尾,例如分数为 -1。
- 使用 a
std::vector代替。
By the way, the logical statement inside your loop will always be true. Are you sure you didn't mean to use &&instead of ||?
顺便说一句,循环中的逻辑语句将始终为真。你确定你不是要使用&&代替||吗?

