Printing an interger's value inside an array

How can I print an array that is based on values of integers inside of it ?
How can I print an array that is based on values of integers inside of it ?

Do you mean you want to display the content of a C-style array but you don’t know its size?

If that’s the question, I usually follow one of these two solutions:
a) I do *not* use C-style arrays, unless I’m forced by a gun aimed at my head - but even in that case, I hesitate :-)
I use vectors instead.

b) I try to discover my array size by sizeof.
The size of an array is equal to the number of elements it contains, so it’s its overall size divided by the size of one element.

For example:
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
#include <iostream>
#include <limits>

void waitForEnter();


int main()
{
    int my_int_array[] = {1, 2, 3, 4, 5};
    
    size_t my_int_array_size = sizeof(my_int_array) / sizeof(int);
    
    for(size_t i{0}; i<my_int_array_size; i++) {
        std::cout << "my_int_array[" << i << "]: " << my_int_array[i]
                  << "; ";
    }
    waitForEnter();
    return 0;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

maybe he means
a = 3
b = 9
??
for that you will need std::map...and I am no expert in std::map
Topic archived. No new replies allowed.