Skip to content

Instantly share code, notes, and snippets.

@mandyedi
Created January 12, 2015 22:42
Show Gist options
  • Save mandyedi/f6d860dfc9c43fee3809 to your computer and use it in GitHub Desktop.
Save mandyedi/f6d860dfc9c43fee3809 to your computer and use it in GitHub Desktop.
c++: template: 2d array
// http://stackoverflow.com/questions/8767166/passing-2d-array-to-function/17569578#17569578
#include <iostream>
template <size_t rows, size_t cols>
void process_2d_array_template( int( &array )[rows][cols] )
{
for ( size_t i = 0; i < rows; ++i )
{
std::cout << i << ": ";
for ( size_t j = 0; j < cols; ++j )
{
std::cout << array[i][j] << '\t';
}
std::cout << std::endl;
}
}
int main( int argc, char *argv[] )
{
int array[3][3] = {
{ 2, 4, 1 },
{ 5, 1, 7 },
{ 7, 1, 6 }
};
process_2d_array_template( array );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment