Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Answer of exercise #9: No for-loops #15

 

This is the answer of Exercise #9: No for-loops.

 

 

 

 

 

Question #15: write to std::cout

 

Replace the for-loop. You will need:

 

 

#include <vector>

void CoutVector(std::vector<int>& v)
{
  const int sz = static_cast<int>(v.size());
  for (int i=0; i!=sz; ++i)
  {
    std::cout << v[i] << '\n';
  }
}

 

 

 

 

 

Answer

 

#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>

//From http://www.richelbilderbeek.nl/CppCoutVector.htm
template <class T>
void CoutVector(const std::vector<T>& v)
{
  std::copy(v.begin(),v.end(),std::ostream_iterator<T>(std::cout,"\n"));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict