Skip to content

Latest commit

 

History

History
27 lines (17 loc) · 2.09 KB

CppConvertMatrix.md

File metadata and controls

27 lines (17 loc) · 2.09 KB

#(C++) ConvertMatrix

Container code snippet that converts a std::vector<std::vector<X> > to std::vector<std::vector<Y> > using static_cast.

 


#include <cassert> #include <vector> //From http://www.richelbilderbeek.nl/CppConvertMatrix.htm template <class Source, class Target> const std::vector<std::vector<Target> > ConvertMatrix(   const std::vector<std::vector<Source> >& v) {   const int maxy = static_cast<int>(v.size());   assert(maxy>0);   const int maxx = static_cast<int>(v[0].size());   std::vector<std::vector<Target> > t(maxy, std::vector<Target>(maxx));   for (int y=0; y!=maxy; ++y)   {     for (int x=0; x!=maxx; ++x)     {       t[y][x] = static_cast<Target>(v[y][x]);     }   }   return t; }