Skip to content

Latest commit

 

History

History
78 lines (43 loc) · 1.97 KB

CppExerciseNoForLoopsAnswer15.md

File metadata and controls

78 lines (43 loc) · 1.97 KB

 

 

 

 

 

 

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")); }