range based loop question

I am trying to get output from a range based loop. If I use a 'standard' approach - iterating with a standard for loop it works as expected. I can see that. However I just wanted to try with a range based loop, but the output is not as expected. I understand the output, but I would like the value of the auto iterator. If you look at my code sample it should be clear...

Thanks in advance for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  
#include <iostream>
#include <array>
#include <cmath>

int main(int argc, const char * argv[]) {

    std::array<bool, 60> LotteryNumbers = {false};
    
    srand(static_cast<unsigned int>(time(NULL)));
    
    int line = 6;
    
    while(line > 0){
        int number = 1 + rand() % 59;
        if(!LotteryNumbers[number]){
            LotteryNumbers[number] = true;
            line--;
        }
    }
    
    
    for(int it = 1; it < 60; it++){
        if(LotteryNumbers[it]){
            std::cout << it << " ";
        }
    }
    
    std::cout << std::endl;
    
    for( const auto& it : LotteryNumbers){
        if(it){
            std::cout << it << " ";
        }
    }
    
    
    std::cout << std::endl;
    
    return 0;
}
Hi,

The purpose of the range based for is to easily have access to de-referenced elements in a container. So your goal of trying to have access to a containers subscript isn't a suitable use for ranged based for.

Why are you storing these numbers as bool? Why not a vector of the number themselves? Maybe you want a std::bitset instead - if your goal is to save space?
Thanks for the reply. I maybe wrong, but the reason for the bool is in case the number has been randomly generated before, it will generate another random number and check for true again. If false it changes that bool at that index to true.

Basically trying to do it with a range based loop doesnt make sense. It does work if I create a variable int value = 0;
Then increment this with the loop, then when the iterator is true(1) print this variable.
Bit rubbish really.

Thanks for your help!
You could try changing line 33 to
std::cout << &it - &LotteryNumbers[0] << " ";
and relying on the elements of LotteryNumbers being in successive positions in memory.

I don't think it's what range-based for loops were designed for, though.
I've changed it to the following and it appears to work...
Also checked for duplicates by changing
int number = 1 + rand() % 59;

to

int number = 1 + rand() % 10;

to make sure numbers will be drawn more than once and that is ok.

Thanks for your help!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <iostream>
#include <array>
#include <cmath>
#include <iterator>

int main(int argc, const char * argv[]) {

    std::array<int, 60> LotteryNumbers = { 0 };
    
    srand(static_cast<unsigned int>(time(NULL)));
    
    int line = 6;
    
    while(line > 0){
        int number = 1 + rand() % 59;
        //std::cout << "in loop" << std::endl;
        if(LotteryNumbers[number] == 0){
            LotteryNumbers[number] = number;
            line--;
        }
    }
    
    
    for(int it = 1; it < 60; it++){
        if(LotteryNumbers[it] != 0){
            std::cout << it << " ";
        }
    }
    
    std::cout << std::endl;
    
    for( const auto it : LotteryNumbers){
        if(it != 0){
            std::cout << it << " ";
        }
    }
    
    
    std::cout << std::endl;
    
    return 0;
}

Thanks everyone - below is my working code...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <vector>
#include <cmath>

int main(int argc, const char * argv[]) {

    std::vector<int> LotteryNumbers(60, 0);
    
    srand(static_cast<unsigned int>(time(NULL)));
    
    int line = 6;
    
    while(line > 0){
        int number = 1 + rand() % 59;
        //std::cout << "in loop" << std::endl;
        if(LotteryNumbers[number] == 0){
            LotteryNumbers[number] = number;
            line--;
        }
    }
    
    
    for(int it = 1; it < 60; it++){
        if(LotteryNumbers[it] != 0){
            std::cout << it << " ";
        }
    }
    
    std::cout << std::endl;
    
    for( const auto& it : LotteryNumbers){
        if(it != 0){
            std::cout << it << " ";
        }
    }
    
    
    std::cout << std::endl;
    
    return 0;
}

Topic archived. No new replies allowed.