简体   繁体   中英

loop through an array in c++

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.

So I dont know what my for loop would look like:

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!

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.

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 scoreCount variable with the number of real values put in there, simply use that in your loop:

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:

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++;
}

Actually the sizeof() should be done like this:

sizeof(scores) / sizeof(scores[0])

And this will give you the total element numbers of the array.

When you populate the scores array, 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:

// 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::vector instead.

By the way, the logical statement inside your loop will always be true. Are you sure you didn't mean to use && instead of || ?

If you really want to use a container with a fixed size, use std::array for modern C++ instead of a C-array:

#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 :

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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