Idont Reallywolf Idont Reallywolf - 3 days ago 6
C++ Question

Printing Vectors - Empty output

I just begun with C++ and im learning about vectors. made a loop to add values into the vector table and then tried to print it out by using:

std::copy(multi_array.begin(), multi_array.end(), std::ostream_iterator<char>(std::cout, " "));


but that prints an empty string on the console window.

here's the code:

vector<int> multi_array;
for(int v=0;v <= 3; v++){
multi_array.push_back(v);
}
std::copy(multi_array.begin(), multi_array.end(), std::ostream_iterator<char>(std::cout, " "));

Answer

You need

std::ostream_iterator<int>(std::cout, " ")
                      ^^^

to write ints. The first template argument must be the type to be printed.

To avoid future trouble like this check out the docs.

Comments