二维动态数组 [英] Two-Dimensional Dynamic Arrays
问题描述
我需要知道如何使下面提到的解决方案起作用。
解决方案来自gbayles 2001年1月29日,下午12:50,链接如下:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
http://groups.google.com/group/comp....c43260a5310?hl
另一种方法是创建一维数组并自己处理索引(index = row * row_size + col)。这很容易在模板类中实现,模板类可以创建任何元素类型和尺寸数量的动态分配的多维数组。
http://groups.google.com/group/comp....c43260a5310?hl
Another way is to create a one dimensional array and handle the
indexing yourself (index = row * row_size + col). This is readily
implemented in template classes that can create dynamically allocated
multi-dimensional arrays of any element type and number of
dimensions.
创建一个允许
一维动态数组的模板的语法是什么,使用传统的
语法来访问二维数组?我在想这个
必须是某种操作符[]重载。
谢谢,
Peter Olcott >
推荐答案
Peter Olcott写道:
Peter Olcott wrote:
我需要知道如何使下面提到的解决方案起作用。
解决方案来自gbayles 2001年1月29日下午12:50,链接提供如下:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
http://groups.google.com/group/comp....c43260a5310?hl
另一种方法是创建一维数组并自己处理索引(index = row * row_size + col)。这很容易在模板类中实现,模板类可以创建动态分配的任何元素类型和尺寸数量的多维数组。
http://groups.google.com/group/comp....c43260a5310?hl
Another way is to create a one dimensional array and handle the
indexing yourself (index = row * row_size + col). This is readily
implemented in template classes that can create dynamically allocated
multi-dimensional arrays of any element type and number of
dimensions.
什么是否会创建一个允许一维动态数组的模板的语法,使用传统的
语法来访问二维数组?我在想这个
必须是某种operator []重载。
What would be the syntax for created a template that allowed a
one dimensional dynamic array, to be addressed using the conventional
syntax for accessing a two dimensional array? I am thinking that this
must be some sort of operator[] overloading.
http://groups.google.com/group/comp....4f1d9bd?hl=en&
我知道我回答了这个以前 - 我认为还有一个常见问题解答..
这是该帖子的示例代码。
#include< vector>
模板< typename w_elem_type>
类矩阵
{
public:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std ::矢量< w_elem_type> m_data;
矩阵(t_Size i_columns = 0,t_Size i_rows = 0)
:m_columns(i_columns),
m_rows( i_rows),
m_data(i_columns * i_rows)
{
}
w_elem_type * operator [](t_Size i_index)
{
return& (m_data [i_index * m_rows]);
}
模板< typename w_Type,int w_columns,int w_rows>
matrix(const w_Type(& i_array)[w_columns] [w_rows])
:m_columns(w_columns),
m_rows(w_rows),
m_data(&(i_array [0] [0]),&(i_array [w_columns-1] [w_rows]))
{
} >
};
#include< iostream>
double array [3] [4] = {
{1.0,2.0,3.3,4.4},
{1.0,2.0,3.3,4.4},
{1.0,2.0, 3.3,4.5},
};
int main()
{
矩阵< float> mat1(3,4);
矩阵< float> mat2;
矩阵< float> mat3(数组);
mat2 = mat3;
std :: cout<< mat2 [2] [3]<< " \ n";
}
http://groups.google.com/group/comp....4f1d9bd?hl=en&
I knew I answered this once before - I think there is an FAQ as well..
Here is the example code from that posting.
#include <vector>
template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std::vector<w_elem_type> m_data;
matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
: m_columns( i_columns ),
m_rows( i_rows ),
m_data( i_columns * i_rows )
{
}
w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}
template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )
{
}
};
#include <iostream>
double array[3][4] = {
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.5 },
};
int main()
{
matrix<float> mat1( 3, 4 );
matrix<float> mat2;
matrix<float> mat3( array );
mat2 = mat3;
std::cout << mat2[2][3] << "\n";
}
这看起来是一个很好的解决方案。我需要绝对的顶级性能。
您提到:
您可以使用的一些广泛的矩阵库
和非常高效的如果尺寸
已知。
您能否提供链接或其他参考资料?
再次感谢您的支持顶级协助。
" Gianni Mariani" < GI ******* @ mariani.ws>在留言中写道
news:q4 ****************************** @ speakeasy.ne t。 ..
That looks like an excellent solution. I need absolutely top performance.
You mentioned :
some extensive matrix libraries you could use
and ones that are very efficient if the dimensions
are known.
Could you provide me a link or other reference to these?
Thanks again for your top notch assistance.
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:q4******************************@speakeasy.ne t...
Peter Olcott写道:
Peter Olcott wrote:
我需要知道如何让下面提到的解决方案起作用。
解决方案来自gbayles 2001年1月29日下午12:50,链接提供如下:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
http://groups.google.com/group/comp....c43260a5310?hl
另一种方法是创建一维数组并自己处理索引(index = row * row_size + col)。这很容易在模板类中实现,模板类可以创建动态分配的任何元素类型和尺寸数量的多维数组。
http://groups.google.com/group/comp....c43260a5310?hl
Another way is to create a one dimensional array and handle the
indexing yourself (index = row * row_size + col). This is readily
implemented in template classes that can create dynamically allocated
multi-dimensional arrays of any element type and number of
dimensions.
什么是否会创建一个允许一维动态数组的模板的语法,使用传统的
语法来访问二维数组?我在想这个
必须是某种operator []重载。
What would be the syntax for created a template that allowed a
one dimensional dynamic array, to be addressed using the conventional
syntax for accessing a two dimensional array? I am thinking that this
must be some sort of operator[] overloading.
http://groups.google.com/group/comp....4f1d9bd?hl=en&
我知道我之前回答过一次 - 我认为还有一个常见问题解答..
以下是该帖子的示例代码。
#include< vector>
模板< ; typename w_elem_type>
类矩阵
{
公开:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std :: vector< w_elem_type> m_data;
矩阵(t_Size i_columns = 0,t_Size i_rows = 0)
:m_columns(i_columns),
m_rows(i_rows),
m_data(i_columns * i_rows )
{
}
w_elem_type * operator [](t_Size i_index)
{
返回& (m_data [i_index * m_rows]);
}
模板< typename w_Type,int w_columns,int w_rows>
矩阵(const w_Type(& i_array)[w_columns] [w_rows])
:m_columns(w_columns),
m_rows(w_rows),
m_data(&(i_array [0] [0]),&(i_array [w_columns-1] [w_rows]))
};
#include< iostream>
双数组[ 3] [4] = {
{1.0,2.0,3.3,4.4},
{1.0,2.0,3.3,4.4},
{1.0,2.0,3.3,4.5},
};
int main()
{
矩阵< float> mat1(3,4);
矩阵< float> mat2;
矩阵< float> mat3(数组);
mat2 = mat3;
std :: cout<< mat2 [2] [3]<< " \ n";
}
http://groups.google.com/group/comp....4f1d9bd?hl=en&
I knew I answered this once before - I think there is an FAQ as well..
Here is the example code from that posting.
#include <vector>
template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std::vector<w_elem_type> m_data;
matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
: m_columns( i_columns ),
m_rows( i_rows ),
m_data( i_columns * i_rows )
{
}
w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}
template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )
{
}
};
#include <iostream>
double array[3][4] = {
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.5 },
};
int main()
{
matrix<float> mat1( 3, 4 );
matrix<float> mat2;
matrix<float> mat3( array );
mat2 = mat3;
std::cout << mat2[2][3] << "\n";
}
哦,是的,我只需要快速寻址元素
Matrix的
。我可能会在矩阵行上下移动
,大约是移动矩阵列的五倍。
" Gianni Mariani" < GI ******* @ mariani.ws>在留言中写道
news:q4 ****************************** @ speakeasy.ne t。 ..
Oh yeah, I only need very fast addressing of the elements
of the Matrix. I will likely be moving up or down matrix rows
about five times as often as moving across matrix columns.
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:q4******************************@speakeasy.ne t...
Peter Olcott写道:
Peter Olcott wrote:
我需要知道如何让下面提到的解决方案起作用。
解决方案来自gbayles 2001年1月29日下午12:50,链接提供如下:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
http://groups.google.com/group/comp....c43260a5310?hl
另一种方法是创建一维数组并自己处理索引(index = row * row_size + col)。这很容易在模板类中实现,模板类可以创建动态分配的任何元素类型和尺寸数量的多维数组。
http://groups.google.com/group/comp....c43260a5310?hl
Another way is to create a one dimensional array and handle the
indexing yourself (index = row * row_size + col). This is readily
implemented in template classes that can create dynamically allocated
multi-dimensional arrays of any element type and number of
dimensions.
什么是否会创建一个允许一维动态数组的模板的语法,使用传统的
语法来访问二维数组?我在想这个
必须是某种operator []重载。
What would be the syntax for created a template that allowed a
one dimensional dynamic array, to be addressed using the conventional
syntax for accessing a two dimensional array? I am thinking that this
must be some sort of operator[] overloading.
http://groups.google.com/group/comp....4f1d9bd?hl=en&
我知道我之前回答过一次 - 我认为还有一个常见问题解答..
以下是该帖子的示例代码。
#include< vector>
模板< ; typename w_elem_type>
类矩阵
{
公开:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std :: vector< w_elem_type> m_data;
矩阵(t_Size i_columns = 0,t_Size i_rows = 0)
:m_columns(i_columns),
m_rows(i_rows),
m_data(i_columns * i_rows )
{
}
w_elem_type * operator [](t_Size i_index)
{
返回& (m_data [i_index * m_rows]);
}
模板< typename w_Type,int w_columns,int w_rows>
矩阵(const w_Type(& i_array)[w_columns] [w_rows])
:m_columns(w_columns),
m_rows(w_rows),
m_data(&(i_array [0] [0]),&(i_array [w_columns-1] [w_rows]))
};
#include< iostream>
双数组[ 3] [4] = {
{1.0,2.0,3.3,4.4},
{1.0,2.0,3.3,4.4},
{1.0,2.0,3.3,4.5},
};
int main()
{
矩阵< float> mat1(3,4);
矩阵< float> mat2;
矩阵< float> mat3(数组);
mat2 = mat3;
std :: cout<< mat2 [2] [3]<< " \ n";
}
http://groups.google.com/group/comp....4f1d9bd?hl=en&
I knew I answered this once before - I think there is an FAQ as well..
Here is the example code from that posting.
#include <vector>
template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;
t_Size m_columns;
t_Size m_rows;
std::vector<w_elem_type> m_data;
matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
: m_columns( i_columns ),
m_rows( i_rows ),
m_data( i_columns * i_rows )
{
}
w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}
template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )
{
}
};
#include <iostream>
double array[3][4] = {
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.5 },
};
int main()
{
matrix<float> mat1( 3, 4 );
matrix<float> mat2;
matrix<float> mat3( array );
mat2 = mat3;
std::cout << mat2[2][3] << "\n";
}
这篇关于二维动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!