Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::for_each and two-dimensional array

Tags:

c++

stl

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;
}
like image 306
Avinash Avatar asked Nov 25 '25 14:11

Avinash


2 Answers

I think the solution is custom function object. Try something like this:

struct process_col
{
      int row_index;
      int col_index;
      process_col(int r) : row_index(r), col_index(0){}
      void operator()(short & data)
      {
          //use row_index, col_index, and data at this indices

           std::cout << "Value at (" <<row_index <<"," << col_index << ") is " << data << std::endl;

           col_index++; //at the bottom
      }
};

struct process_row
{
      int row_index;
      process_row() : row_index(0){}
      void operator()(std::vector<short> & row)
      {
         std::for_each(row.begin(), row.end(), process_col(row_index));
         row_index++;
      }
};

And then use it as:

std::for_each( t.begin(), t.end(), process_row());

Online Demo : http://ideone.com/Dft8X

Output:

Value at (0,0) is 0
Value at (0,1) is 1
Value at (0,2) is 2
Value at (0,3) is 3
Value at (0,4) is 4
Value at (1,0) is 5
Value at (1,1) is 6
Value at (1,2) is 7
Value at (1,3) is 8
Value at (1,4) is 9
Value at (2,0) is 10
Value at (2,1) is 11
Value at (2,2) is 12
Value at (2,3) is 13
Value at (2,4) is 14
Value at (3,0) is 15
Value at (3,1) is 16
Value at (3,2) is 17
Value at (3,3) is 18
Value at (3,4) is 19
Value at (4,0) is 20
Value at (4,1) is 21
Value at (4,2) is 22
Value at (4,3) is 23
Value at (4,4) is 24
like image 106
Nawaz Avatar answered Nov 27 '25 05:11

Nawaz


c++98

void SomeThingElse(int row, int col)
{  
  // Impl.
}

struct Fun
{
     private:
        int col_m; 
        int row_m;
     public:
        Fun(int row) : row_m(row) { }
        void operator()(int x) const
        {
            SomeThingElse(row_m, x);
        }
};
void SomeFun(const std::vector<short>& cols)
{
   static int row = 0;
   ++row;
   std::for_each( cols.begin(), cols.end(), Fun(row));
}    


std::for_each(table_t.begin(), table_t.end(), SomeFun);

c++11. ( Just to show how easy it would be ). ( EDIT )

int row = 0;
std::for_each( begin(table_t), end(table_t), [&]( const std::vector<short>& col)
{
    row++;
    int temp = row;
    std::for_each( begin(col), end(col), [=](int x )
    {
          fn(temp, x);   
    });
});
like image 27
Jagannath Avatar answered Nov 27 '25 06:11

Jagannath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!