(C++) CoutContainer
January 8, 2018 · View on GitHub
(C++) CoutContainer
Container code snippet to std::cout a container, using the algorithm std::copy and the std::ostream_iterator.
#include <vector> #include <iterator> #include <iostream> #include <ostream> //From http://www.richelbilderbeek.nl/CppCoutContainer.htm template <class Container> void CoutContainer(const Container& c) { std::copy(c.begin(),c.end(), std::ostream_iterator<typename Container::value_type>(std::cout,"\n")); }
When using the IDE C++ Builder 6.0, remove the keyword typename to prevent a compile error.
CoutContainer test
#include <vector> #include <iterator> #include <iostream> #include <ostream> //From http://www.richelbilderbeek.nl/CppCoutContainer.htm template <class Container> void CoutContainer(const Container& c) { std::copy(c.begin(),c.end(), std::ostream_iterator<typename Container::value_type>(std::cout,"\n")); } int main() { //Create a vector std::vector<int> v; v.push_back(1); v.push_back(4); v.push_back(9); v.push_back(16); v.push_back(25); //Show it on screen using CoutContainer CoutContainer(v); }