Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(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;
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict