std :: for_each和二维数组

时间:2011-12-05 07:21:32

标签: c++ stl

我使用以下定义定义了二维数组 typedef std::vector<std::vector<short> > table_t;

我可以对此数组使用std::for_each,我想将rowcol作为参数传递给function

或者有没有办法在row

中识别colfunction

以下是获得更多想法的代码。

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

3 个答案:

答案 0 :(得分:2)

我认为解决方案是自定义函数对象。尝试这样的事情:

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

然后将其用作:

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

在线演示:http://ideone.com/Dft8X

输出:

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

答案 1 :(得分:2)

<强> 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。(只是为了表明它有多容易)。 (编辑)

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

答案 2 :(得分:1)

严格地说,我认为你想要std::array<std::array<short, 5> ,5>矢量矢量(确保所有行长度相等并优化存储)。

如果您的编译器已经支持模板别名,您可以使用一个来获得“漂亮”#声明:

template <class T, size_t R, size_t C>   
     using matrix = std::array<std::array<T, C>, R>;    

// use it simply like:
matrix<short, 5, 5> t; 

这是一个粗略的草稿,可以同时做到这两点,基于C ++ 11

#include <array>
#include <vector>
#include <iostream>

template <typename M, typename F>
    void for_matrix(M& matrix, F f)
{
    size_t r = 0;
    for (auto& row : matrix)
    {
        size_t c = 0;
        for (auto& cell : row)
            f(cell, r, c++);
        r++;
    }
}


void sample_function(short& data, size_t row, size_t col)
{
    std::cout << "(" << row << ", " << col << "): " << data << std::endl;
}

int main()
{
    // based on vector:
    typedef std::vector<std::vector<short> > table_t;
    table_t t = table_t(5, std::vector<short>(5));

    for_matrix(t, sample_function);
    for_matrix(t, [] (short&i,size_t,size_t) { std::cout << "lambda: " << i << std::endl; });

    // based on std::array:
    std::array<std::array<short, 3>, 4> a;
    for_matrix(a, [] (short&i,size_t,size_t) { i = 0; });
    for_matrix(a, sample_function);
}