0

What is the correct way in C++ to output a whole container (in this case the targets are vectors and arrays)? Possibly I'd like to put spaces in it, or sort them like this:

1: (first element)

2: (second element)

3: (third element)

and so on...?

Adam
  • 1,476
  • 2
  • 16
  • 22
  • 5
    Possible duplicate of [How to print out the contents of a vector?](http://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector) – Justin Apr 10 '17 at 20:28

1 Answers1

0

The answer is by looping on each element:

// Array
for (int i = 0; i < size_of_array; ++i) {
    std::cout << i << ": " << array[i] << "\n";
}

The same for a vector would be the same, but replace i < size_of_array; with i < name_of_vector.size();

Tommaso Thea Cioni
  • 474
  • 12
  • 18