I have defined two dimensional array using following definition
typedef std::vector<std::vector<short> > table_t;
Can I use std::for_each for this array, I want to pass row and col as a parameter to the function
Or is there a way to identify row and col in the function
following is the code to get more idea.
#include <vector>
#include <iostream>
#include <algorithm>
typedef std::vector<std::vector<short> > table_t;
void myfunction (int i) {
std::cout << " " << i;
}
int main ( int argc , char **argv) {
table_t t = table_t(5, std::vector<short>(5));
int counter = 0;
for (size_t row = 0; row < 5; ++row)
for (size_t col = 0; col < 5; ++col)
t[row][col] = counter++;
std::for_each( t.begin(), t.end(), myfunction);
return 0;
}
I think the solution is custom function object. Try something like this:
And then use it as:
Online Demo : http://ideone.com/Dft8X
Output: