Skip to content

Latest commit

 

History

History
75 lines (42 loc) · 3.57 KB

CppIsSquare.md

File metadata and controls

75 lines (42 loc) · 3.57 KB

 

 

 

 

 

 

IsSquare is a std::vector code snippet to check if a 2D-std::vector is square.

 

 

 

 

 

Qt Creator source code

 

 


#include <cassert> #include <iterator> #include <vector> #include <boost/foreach.hpp> //From http://www.richelbilderbeek.nl/CppIsSquare.htm template <typename T> bool IsSquare(const std::vector<std::vector<T> >& v) {   assert(!v.empty());   BOOST_FOREACH(std::vector<T> row, v)   {     if (row.size()!=v.size()) return false;   }   return true; } int main() {   std::vector<std::vector<int> > v;   v.resize(4);   BOOST_FOREACH(std::vector<int>& row, v)   {     row.resize(4);   }   assert(IsSquare(v)==true);   v[3].resize(5);   assert(IsSquare(v)==false); }

 

 

 

 

 

C++ Builder source code

 


//From http://www.richelbilderbeek.nl/CppIsSquare.htm template <class T> bool IsSquare(const std::vector<std::vector<T> >& v) {   assert(!v.empty());   const int sz = static_cast<int>(v.size());   const std::vector<std::vector<T> >::const_iterator j = v.end();   std::vector<std::vector<T> >::const_iterator i = v.begin();   for ( ; i!=j; ++i)   {     if (sz != static_cast<int>(i->size())) return false;   }   return true; }