}; #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";} 什么是否会创建一个允许一维动态数组的模板的语法,使用传统的语法来访问二维数组?我在想这个必须是某种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"; } 什么是否会创建一个允许一维动态数组的模板的语法,使用传统的语法来访问二维数组?我在想这个必须是某种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"; } 这篇关于二维动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 17:21