简体   繁体   English

std :: for_each和二维数组

[英]std::for_each and two-dimensional array

I have defined two dimensional array using following definition typedef std::vector<std::vector<short> > table_t; 我已经使用以下定义定义了二维数组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 我可以在此数组中使用std::for_each吗,我想将rowcol作为参数传递给function

Or is there a way to identify row and col in the function 还是有一种方法可以识别function rowcol

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: 尝试这样的事情:

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 在线演示: 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

c++98 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. 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);   
    });
});

Strictly speaking, I think you'd want std::array<std::array<short, 5> ,5> intead of the vector-of-vector (making sure all rows are of equal length and optimizing storage). 严格来说,我认为您希望将std::array<std::array<short, 5> ,5>插入向量向量中(确保所有行的长度相等并优化存储)。

If your compiler already supports template aliases you can use one to get 'pretty' declaration: 如果您的编译器已经支持模板别名 ,则可以使用一个来获取“漂亮”声明:

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; 

Here is a rough draft that can do both, based on C++11 这是一个基于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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM