for_each()和二维数组 [英] for_each() and two-dimensional array

查看:94
本文介绍了for_each()和二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,在我的一个课程中,我有这个成员变量:

Tile *** tiles_;其中Tile是我的另一个类。

Hello, in one of my classes I have this member variable:
Tile ***tiles_; where Tile is another one of my classes.


>从这个指向指针到指针到Tile的指针我分配了两个$
>From this pointer-to-pointer-to-pointer-to-Tile I allocate a two-



Tile-pointers的维数组(尺寸大小相等)。

最后,我为2d数组中的每个指针分配一个Tile对象。

无论如何,我经常需要遍历整个阵列并执行

操作,所以直到现在我已经使用了很多这样的结构:


for(int y = 0; y< num_rows; ++ y)

{

for(int x = 0; x< num_cols ; ++ x)

{

//做点什么

}

}


我在考虑是否可以使用std :: for_each(),如果它可以使用

是一个好主意。我制作了以下测试程序,它在两个不同的编译器上打印1,2,

3,4,所以它看起来是正确的,并且符合b $ b标准,但我想要听听你有什么话要说

吧。

#include< iostream>


使用命名空间std;


struct foobar

{

void operator()(const int i)

{

cout<< i<<结束;

}

};


int main()

{

const int rows = 2;

const int cols = 2;

int foo [cols] [rows] = {{1,2},{3 ,4}};

for_each(&(foo [0] [0]),&(foo [0] [0])+(rows * cols),foobar());

}


- Eric

dimensional array of Tile-pointers (dimensions are of equal sizes).
Last, I allocate a Tile object for each pointer in the 2d-array.
Anyway, I often need to traverse the entire array and perform
operations so until now I''ve used alot of constructs like this:

for (int y = 0; y < num_rows; ++y)
{
for (int x = 0; x < num_cols; ++x)
{
// Do something
}
}

I was thinking of if I can use std::for_each() instead and if it would
be a good idea. I made the following test program and it prints 1, 2,
3, 4 on two different compilers so it seems to be correct, and
standards-compliant but I wanted to hear what you have to say about
it.
#include <iostream>

using namespace std;

struct foobar
{
void operator()(const int i)
{
cout << i << endl;
}
};

int main()
{
const int rows = 2;
const int cols = 2;
int foo[cols][rows] = {{1, 2}, {3, 4}};
for_each(&(foo[0][0]), &(foo[0][0]) + (rows * cols), foobar());
}

- Eric

推荐答案

Eric Lilja写道:
Eric Lilja wrote:

您好,在我的一个班级中我有这个成员变量:

Tile *** tiles_;其中Tile是我的另一个类。
Hello, in one of my classes I have this member variable:
Tile ***tiles_; where Tile is another one of my classes.

>>从这个指针到指针到指针到Tile我分配一个两个
>>From this pointer-to-pointer-to-pointer-to-Tile I allocate a two-



Tile-pointers的维数组(尺寸大小相等)。

最后,我为2d中的每个指针分配一个Tile对象-array。

无论如何,我经常需要遍历整个阵列并执行

操作,所以直到现在我已经使用了很多像这样的结构:


for(int y = 0; y< num_rows; ++ y)

{

for(int x = 0; x< ; num_cols; ++ x)

{

//做点什么

}

}


我在考虑是否可以使用std :: for_each(),如果它可以使用
是个好主意。我制作了以下测试程序,它在两个不同的编译器上打印1,2,

3,4,所以它看起来是正确的,并且符合b $ b标准,但我想要听听你有什么话要说

吧。

#include< iostream>


使用命名空间std;


struct foobar

{

void operator()(const int i)

{

cout<< i<<结束;

}

};


int main()

{

const int rows = 2;

const int cols = 2;

int foo [cols] [rows] = {{1,2},{3 ,4}};

for_each(&(foo [0] [0]),&(foo [0] [0])+(rows * cols),foobar());

}


- Eric

dimensional array of Tile-pointers (dimensions are of equal sizes).
Last, I allocate a Tile object for each pointer in the 2d-array.
Anyway, I often need to traverse the entire array and perform
operations so until now I''ve used alot of constructs like this:

for (int y = 0; y < num_rows; ++y)
{
for (int x = 0; x < num_cols; ++x)
{
// Do something
}
}

I was thinking of if I can use std::for_each() instead and if it would
be a good idea. I made the following test program and it prints 1, 2,
3, 4 on two different compilers so it seems to be correct, and
standards-compliant but I wanted to hear what you have to say about
it.
#include <iostream>

using namespace std;

struct foobar
{
void operator()(const int i)
{
cout << i << endl;
}
};

int main()
{
const int rows = 2;
const int cols = 2;
int foo[cols][rows] = {{1, 2}, {3, 4}};
for_each(&(foo[0][0]), &(foo[0][0]) + (rows * cols), foobar());
}

- Eric



您的代码假设2D数组是连续的在内存中

(实际上它将2D数组看作是一维数组)。对于您编写的测试代码,这是真的

但不一定适用于您的

实际代码。


正确地做到这一点的方法是停止使用所有这些指针

(毕竟你正在编写C ++)。 For_each不需要指针,而是

它可以使用迭代器。编写一个2D数组类,它有一个方法,即
返回一个迭代器,它允许你迭代2D数组,如果它是1D,则为
。这样的东西


class TileArray_2D

{

TileArray_2D(int rows,int cols);

TileIterator_1D begin_1d();

TileIterator_1D end_1d();

};


class TileIterator_1D

{

Tile * operator *();

TileIterator_1D& operator ++();

};

TileArray_2D foo(rows,cols);

for_each(foo.begin_1d(),foo.end_1d( ),foobar());


我认为你需要一本关于C ++和标准模板的好书。
库。 Nicolai Josuttis是你的男人。


john

You code makes the assumption that the 2D array is contiguous in memory
(in effect it treats a 2D array as if it were a 1D array). This is true
for the test code you''ve written but is not necessarily true for your
real code.

The way to do this correctly would be to stop using all those pointers
(you''re writing C++ after all). For_each doesn''t need pointers, instead
it can use iterators. Write a 2D array class, that has an method that
returns an iterator which allows you to iterate through the 2D array as
if it were 1D. Something like this

class TileArray_2D
{
TileArray_2D(int rows, int cols);
TileIterator_1D begin_1d();
TileIterator_1D end_1d();
};

class TileIterator_1D
{
Tile* operator*();
TileIterator_1D& operator++();
};
TileArray_2D foo(rows, cols);
for_each(foo.begin_1d(), foo.end_1d(), foobar());

I think you''re in need of a good book on C++ and the standard template
library. Nicolai Josuttis is your man.

john


On 23 Maj,07:24,John Harrison< john_androni ...... @ hotmail.comwrote:
On 23 Maj, 07:24, John Harrison <john_androni...@hotmail.comwrote:

Eric Lilja写道:
Eric Lilja wrote:

你好,在我的一个课程中我有这个成员变量:

Tile *** tiles_; Tile是我班上的另一个。
Hello, in one of my classes I have this member variable:
Tile ***tiles_; where Tile is another one of my classes.


>从这个指针到指针指向Tile我分配一个两个 -
>From this pointer-to-pointer-to-pointer-to-Tile I allocate a two-



Tile-pointers的维数组(尺寸大小相等)。

最后,我为每个指针分配一个Tile对象2d-array。

无论如何,我经常需要遍历整个数组并执行

操作,所以直到现在我已经使用了很多这样的结构:

dimensional array of Tile-pointers (dimensions are of equal sizes).
Last, I allocate a Tile object for each pointer in the 2d-array.
Anyway, I often need to traverse the entire array and perform
operations so until now I''ve used alot of constructs like this:


for(int y = 0; y< num_rows; ++ y)

{

for(int x = 0; x< num_cols; ++ x)

{

//做点什么

}

}
for (int y = 0; y < num_rows; ++y)
{
for (int x = 0; x < num_cols; ++x)
{
// Do something
}
}


我在考虑是否可以使用std :: for_each(),如果它可以使用

是个好主意。我制作了以下测试程序,它在两个不同的编译器上打印1,2,

3,4,所以它看起来是正确的,并且符合b $ b标准,但我想要听听你要说的话

吧。

#include< iostream>
I was thinking of if I can use std::for_each() instead and if it would
be a good idea. I made the following test program and it prints 1, 2,
3, 4 on two different compilers so it seems to be correct, and
standards-compliant but I wanted to hear what you have to say about
it.
#include <iostream>


using namespace std;
using namespace std;


struct foobar

{

void operator()(const int i)

{

cout<< i<<结束;

}

};
struct foobar
{
void operator()(const int i)
{
cout << i << endl;
}
};


int main()

{

const int rows = 2;

const int cols = 2;

int foo [cols] [rows] = {{1,2},{3,4}};

for_each(&(foo [0] [0]),&(foo [0] [0])+(rows * cols),foobar());

}
int main()
{
const int rows = 2;
const int cols = 2;
int foo[cols][rows] = {{1, 2}, {3, 4}};
for_each(&(foo[0][0]), &(foo[0][0]) + (rows * cols), foobar());
}


- Eric
- Eric



您的代码假设2D数组在内存中是连续的

(实际上它将2D数组视为一维数组)。对于您编写的测试代码,这是真的

但对于您的

实际代码不一定如此。


You code makes the assumption that the 2D array is contiguous in memory
(in effect it treats a 2D array as if it were a 1D array). This is true
for the test code you''ve written but is not necessarily true for your
real code.



这是让我担心的一件事。

This is one thing that worries me.


>

正确地做到这一点的方法是停止使用所有这些指针

(毕竟你正在编写C ++)。 For_each不需要指针,而是

它可以使用迭代器。编写一个2D数组类,它有一个方法,即
返回一个迭代器,它允许你迭代2D数组,如果它是1D,则为
。像这样的东西
>
The way to do this correctly would be to stop using all those pointers
(you''re writing C++ after all). For_each doesn''t need pointers, instead
it can use iterators. Write a 2D array class, that has an method that
returns an iterator which allows you to iterate through the 2D array as
if it were 1D. Something like this



我完全知道它不需要指针。十分之九,

如果

不多,我将它与迭代器一起使用。但是,我认为能够使用带有for_each()和其他的原始指针非常方便。

I know perfectly well it doesn''t need pointers. Nine times out of ten,
if
not more, I use it with iterators. However, I think that it''s very
convenient to able to use raw pointers with for_each() and others.


>

class TileArray_2D

{

TileArray_2D(int rows,int cols);

TileIterator_1D begin_1d();

TileIterator_1D end_1d();


};


class TileIterator_1D

{

Tile * operator *();

TileIterator_1D& operator ++();


};


TileArray_2D foo(rows,cols);

for_each(foo。 begin_1d(),foo.end_1d(),foobar());
>
class TileArray_2D
{
TileArray_2D(int rows, int cols);
TileIterator_1D begin_1d();
TileIterator_1D end_1d();

};

class TileIterator_1D
{
Tile* operator*();
TileIterator_1D& operator++();

};

TileArray_2D foo(rows, cols);
for_each(foo.begin_1d(), foo.end_1d(), foobar());



我想我会这样做,但我的end()应该返回什么?

I think I will do this, but what should my end() return?


>

我认为你需要一本关于C ++和标准模板的好书。

库。 Nicolai Josuttis是你的男人。
>
I think you''re in need of a good book on C++ and the standard template
library. Nicolai Josuttis is your man.



我已经拥有它。

I have it already.


>

john
>
john



- Eric

- Eric


>>
>>

> TileArray_2D foo(rows,cols);
for_each(foo.begin_1d(),foo.end_1d( ),foobar());
>TileArray_2D foo(rows, cols);
for_each(foo.begin_1d(), foo.end_1d(), foobar());



我想我会这样做,但我的end()应该返回什么?


I think I will do this, but what should my end() return?



并不重要,重要的是如果你有一个迭代器

引用数组的最后一个元素,然后你增加

迭代器,它将等于end()返回的迭代器。


john

Doesn''t really matter, all that matters is that if you have an iterator
that references the last element of the array, and then you increment
that iterator, it will then be equal to the iterator that end() returns.

john


这篇关于for_each()和二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆