print values from vector c++ code example
Example 1: c++ print elements of vector to the console
#include <iostream>
#include <vector>
int main()
{
std::vector<int> myVector = {1, 2, 3, 4, 5, 6};
for(int i = 0; i < myVector.size(); i++)
{
std::cout << myVector[i] << std::endl;
}
}
Example 2: c++ print vector without loop
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if ( !v.empty() ) {
out << '[';
std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
out << "\b\b]";
}
return out;
}